473,695 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Working with floating point values

I'm working on a project that uses floating-point values (doubles), and I'm
being driven crazy by something pretty basic. I understand that it's in the
nature of floating-point calculations to produce values like
0.1000000000000 0003, when what I really want is 0.1. But is there any way to
eliminate that digit at the end? I've tried rounding, but that simply moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks in
advance.

David Veeneman
Foresight Systems
Nov 17 '05 #1
9 1778
David,

I've had a quick go at your problem and the Math.Round method does seem
to work.

static void Main(string[] args)
{

double TestNumber = 0.100000000003;

Console.WriteLi ne ( TestNumber.ToSt ring() );

Console.WriteLi ne ( Math.Round ( TestNumber, 1 ) );
Console.WriteLi ne ( Math.Round ( TestNumber, 5 ) );
Console.ReadLin e();
}

OUTPUT:

0.100000000003
0.1
0.1

Hope this helps

David Veeneman wrote:
I'm working on a project that uses floating-point values (doubles), and I'm
being driven crazy by something pretty basic. I understand that it's in the
nature of floating-point calculations to produce values like
0.1000000000000 0003, when what I really want is 0.1. But is there any way to
eliminate that digit at the end? I've tried rounding, but that simply moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks in
advance.

David Veeneman
Foresight Systems


Nov 17 '05 #2
Just use Decimal.
--
Brian Delahunty
Ireland

http://briandela.com/blog
"David Veeneman" wrote:
I'm working on a project that uses floating-point values (doubles), and I'm
being driven crazy by something pretty basic. I understand that it's in the
nature of floating-point calculations to produce values like
0.1000000000000 0003, when what I really want is 0.1. But is there any way to
eliminate that digit at the end? I've tried rounding, but that simply moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks in
advance.

David Veeneman
Foresight Systems

Nov 17 '05 #3
you could format the number like
String.Format(" {0:#,##0.00;#,# #0.00;0.00}", myDecimal.ToStr ing()) ;
(assuming you want a string at the end)

Ranjeet.
Nov 17 '05 #4
That's what I've always done in the past, but decimals are *much* slower to
process than floating-points. In this project, I'm running some fairly
complex calculations thousands of times each, so the difference between
decimals and doubles is quite noticeable. So, I'm pretty much stuck with
doubles.

"Brian Delahunty" <Br************ @discussions.mi crosoft.com> wrote in
message news:FA******** *************** ***********@mic rosoft.com...
Just use Decimal.
--
Brian Delahunty
Ireland

http://briandela.com/blog
"David Veeneman" wrote:
I'm working on a project that uses floating-point values (doubles), and
I'm
being driven crazy by something pretty basic. I understand that it's in
the
nature of floating-point calculations to produce values like
0.1000000000000 0003, when what I really want is 0.1. But is there any way
to
eliminate that digit at the end? I've tried rounding, but that simply
moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing
with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks
in
advance.

David Veeneman
Foresight Systems

Nov 17 '05 #5
Thanks-- I'd thought about that, but I need to maintain the values as
doubles. And the time it would take to convert to string, then back to
double, would slow my calculations down noticeably.

"ranjeet" <ra************ @gmail.com> wrote in message
news:op******** ******@athwalrt 40.castlewood.c o.uk...
you could format the number like
String.Format(" {0:#,##0.00;#,# #0.00;0.00}", myDecimal.ToStr ing()) ;
(assuming you want a string at the end)

Ranjeet.

Nov 17 '05 #6
KH
Rounding won't help because the some floating point numbers simply can't be
exactly represented in binary. ...
http://docs.sun.com/source/806-3568/...dberg.html#680

If you can't/won't use decimal, you can test conditionals by seeing if a
number is within a range:

double dd = 0.1000000000000 0003;

if (dd > 0.09 && dd < 0.11) // true, although the number could be 0.091
0.109 ...
// but you can increase the precision in the conditional

If you doing arithmitic with floating point numbers usually you just do the
calculations and round before displaying.

"David Veeneman" wrote:
I'm working on a project that uses floating-point values (doubles), and I'm
being driven crazy by something pretty basic. I understand that it's in the
nature of floating-point calculations to produce values like
0.1000000000000 0003, when what I really want is 0.1. But is there any way to
eliminate that digit at the end? I've tried rounding, but that simply moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks in
advance.

David Veeneman
Foresight Systems

Nov 17 '05 #7
Well, I found the canonical 1991 article on the subject: "What Every
Computer Scientist Should Know About Floating-Point Arithmetic", by David
Goldberg. Here is a link to the article in PDF format:

http://www.physics.ohio-state.edu/~d...point_math.pdf
Nov 17 '05 #8
Thanks-- the reference to the Goldberg article is very helpful.
Nov 17 '05 #9
If precision is an issue in your calculation, then you have to use decimal.
otherwise, keep using doubles is fine, and use the right output format to
trim off the rounding error when displaying the results.

"David Veeneman" wrote:
Thanks-- I'd thought about that, but I need to maintain the values as
doubles. And the time it would take to convert to string, then back to
double, would slow my calculations down noticeably.

"ranjeet" <ra************ @gmail.com> wrote in message
news:op******** ******@athwalrt 40.castlewood.c o.uk...
you could format the number like
String.Format(" {0:#,##0.00;#,# #0.00;0.00}", myDecimal.ToStr ing()) ;
(assuming you want a string at the end)

Ranjeet.


Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

687
23436
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
3
3795
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that programmers who don't range- check their data can e.g. multiply two very tiny values and end up with a very large one. This is (AFAICT) quite fine according to the Standard but is certainly a QoI issue and I'd like to raise ours. I believe that it's...
17
2829
by: Jonathan Burd | last post by:
Greetings everyone, Reading about the int64_t types added by C'99, I decided to implement a 64-bit version of atoi for my own library. However, for the test number provided, it isn't doing what it is supposed to do. (I used GCC/MingW32 as the compiler with C'99 enabled. Oh, and there is no overflow-checking in this code!) The code follows: #include <ctype.h>
5
2410
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have the opposite order? The idea is that the two fractions are almost identical and that the error introduced by going to floating point representation is bigger than the exact difference, but different for the two fractions such that it somehow turns...
15
3925
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
5
3348
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
7
2201
by: thisismyidentity | last post by:
Hi all, I am trying to predict the behaviour of floating point load and store operations on integer locations. I ve written a small piece of code having some inline assembly code which I am describing here. ======================================================== #include<stdio.h> #include<stdlib.h>
15
8055
by: Mukesh_Singh_Nick | last post by:
Why does floating point have a rounding error? How to work around it? For example, the following: flaot f = 1234.12345678F; printf("%2f\n", f) //prints 1234.123413 and
6
9035
by: shira | last post by:
Hi, Looking to see if someone might have an explanation for this behavior. Is it a bug? Corruption? I have been able to reproduce the problem with only 2 rows and 1 field. Here is the table: Field1 231.2
0
8567
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9114
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7660
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6490
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3001
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2271
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1973
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.