Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding double values gives incorrect result

swhite76@mapinfo.com
Guest
 
Posts: n/a
#1: Nov 22 '05
I have a section of code that adds some double values and gives an incorrect
result. This occurs with data that isn't really waht I would call high
precision. An example is the following code snippet:

---------------
double a = 2.7;
double b = 2.7;
double c = 0.001;

double result=0;
result=result+a;
result=result+b;
result=result+c;
------------------

After running this code the value of the result variable in the quickwatch
(or watch) window is 5.4010000000000007. How is this possible??

If I do the following:
System.Diagnostics.Debug.WriteLine("Value of result = " + result);

it comes out correctly as 5.401. But I need to be able to compare this to
another variable and the result is wrong.

Any ideas what is going on here

Chris Dunaway
Guest
 
Posts: n/a
#2: Nov 22 '05

re: Adding double values gives incorrect result


On Wed, 25 Aug 2004 02:39:04 -0700, swhite76@mapinfo.com wrote:
[color=blue]
> After running this code the value of the result variable in the quickwatch
> (or watch) window is 5.4010000000000007. How is this possible??
>
>
> Any ideas what is going on here[/color]

The result you're getting is because decimal numbers cannot accurately be
stored in binary. Try using the Decimal data type instead which, I
believe, compensates for this.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 22 '05

re: Adding double values gives incorrect result


swhite76@mapinfo.com <swhite76@mapinfo.com> wrote:[color=blue]
> I have a section of code that adds some double values and gives an incorrect
> result. This occurs with data that isn't really waht I would call high
> precision. An example is the following code snippet:
>
> ---------------
> double a = 2.7;
> double b = 2.7;
> double c = 0.001;
>
> double result=0;
> result=result+a;
> result=result+b;
> result=result+c;
> ------------------
>
> After running this code the value of the result variable in the quickwatch
> (or watch) window is 5.4010000000000007. How is this possible??
>
> If I do the following:
> System.Diagnostics.Debug.WriteLine("Value of result = " + result);
>
> it comes out correctly as 5.401. But I need to be able to compare this to
> another variable and the result is wrong.
>
> Any ideas what is going on here[/color]

See http://www.pobox.com/~skeet/csharp/floatingpoint.html and
http://www.pobox.com/~skeet/csharp/decimal.html

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread


Similar .NET Framework bytes