Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Adding Double Variables Precision

Microsoft Visual C++ .NET 55510-640-8018297-18683

When I attempt to add two double type variables, I receive an incorrect result. For example:

double a,b,c,d;

//Test #1
a = 295.00;
b = 0.14;
c = a + b;

//Test #2
d = 14.00;
d /= 100;

When I debug, c = 295.139999999 and d = 0.13999999

These values should be 295.14 and 0.14, respectively. As a result, these unexpected results are causing my program to run incorrectly.

I have also tried to use rounding functions to set c and d correctly after they have been calculated, but have had no success.

Help on this issue would be greatly appreciated - Thanks.
[699 byte] By [cgibson] at [2007-11-11 8:44:22]
# 1 Re: Adding Double Variables Precision
Looks like a good reason not to use this IDE/compiler.
nspils at 2007-11-11 21:01:02 >
# 2 Re: Adding Double Variables Precision
I guess I should add that this is occurring on the following software version:

Microsoft Development Environment 2002
Version 7.0.9955

Micorsoft .Net Framework 1.0
1.0.3705
cgibson at 2007-11-11 21:02:07 >
# 3 Re: Adding Double Variables Precision
It is a rounding error due to the compiler's floating point implementation. I expect you can find information in MSDN for a work around.

http://msdn1.microsoft.com/en-us/default.aspx
nspils at 2007-11-11 21:03:05 >
# 4 Re: Adding Double Variables Precision
Print 'b' from the first example. I suspect you will see .13999999999999 because this value may be impossible to represent exactly in floating point notation. This should not break your program. Do you compare with an == operation? You should almost never use == on floating point numbers, instead something like

if ( fabs(x-y) <= .00000000001) or similar code.
jonnin at 2007-11-11 21:04:11 >