Adding doubles causes unexpected result - bug?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using .net 2.0 and am having a problem adding doubles:

double x = 63881.97 + 34297.98;

The result of this addition in Visual Studio is 98179.950000000012
This is obviously the incorrect result. However, if I change the 34297.98
to 34297.97, it adds the numbers correctly without the erronious decimal
palces.

Does anyone have any ideas why this may be happening?
Thank you
 
Hi!
Does anyone have any ideas why this may be happening?
Thank you

mmm I guess there is a problem with your cpu ;) In my VS I get the correct
result

greets
Daniel
 
justinsaraceno said:
I'm using .net 2.0 and am having a problem adding doubles:

double x = 63881.97 + 34297.98;

The result of this addition in Visual Studio is 98179.950000000012
This is obviously the incorrect result. However, if I change the 34297.98
to 34297.97, it adds the numbers correctly without the erronious decimal
palces.

Does anyone have any ideas why this may be happening?
Thank you


This issue comes up over and over. Apparently they don't teach classes in
computer hardware any more in the universities. This is just a simple
consequence of how floating-point numbers are stored in binary machines.
Most floating-point numbers are stored as approximations of what might
otherwise be precise rational values. For instance, 7/5 is exactly 1.4 in
decimal notation, but cannot be represented precisely in conventional
hardware floating-point architectures because the .4 part cannot be
expressed as a sum of the powers of two, a requirement on binary machines.
(This specific example is paraphrased from the first citation below)

There's nothing we can do about it, except adopt techniques in our code to
avoid the problems that arise. For single-precision, don't expect more than
six or maybe seven places of precision, and for double-precision don't
expect more than about fourteen or fifteen decimal places of precision.
Anything that's displayed after that is just noise; throw it away. Don't use
the "=" sign to compare arbitrary floating-point numbers for equality -
instead do comparisons using some fuzz, such as Single.Epsilon. Or handle it
through rounding. Or use Decimal type if it works for you. And so on.

Try googling for terms like "floating-point precision", "floating-point
representation", "IEEE floating-point", "floating-point roundoff", and so
forth, and maybe you'll find an explanation of your own.

Here's a starting point:
http://www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html

or try Wikipedia on the topic:
http://en.wikipedia.org/wiki/IEEE_754

which also leads to a bunch of good "see also" articles. Of course there
have been and are other representations besides IEEE 754, but this is
nowadays by far the most common.

Think of this as an adventure in learning.

Tom Dacon
Dacon Software Consulting
 
justinsaraceno said:
I'm using .net 2.0 and am having a problem adding doubles:

double x = 63881.97 + 34297.98;

The result of this addition in Visual Studio is 98179.950000000012
This is obviously the incorrect result. However, if I change the 34297.98
to 34297.97, it adds the numbers correctly without the erronious decimal
palces.

Does anyone have any ideas why this may be happening?

Yup - it's due to how floating point arithmetic works.

The two numbers you're *actually* asking the computer to sum are

63881.97000000000116415321826934814453125
and
34297.9800000000032014213502407073974609375

Those are the closest available double values to your original values.

The closest double to the exact result of that addition is:
98179.9500000000116415321826934814453125

Hence the result you're seeing.

See http://pobox.com/~skeet/csharp/floatingpoint.html for more
information.
 
Back
Top