472,334 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

Adding double values gives incorrect result

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
Jul 21 '05 #1
2 2269
On Wed, 25 Aug 2004 02:39:04 -0700, sw******@mapinfo.com wrote:
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


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.
Jul 21 '05 #2
sw******@mapinfo.com <sw******@mapinfo.com> wrote:
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


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

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

Similar topics

0
by: Danny Winslow | last post by:
I get an unexpected result when I add two negative numbers in PHP on SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using PHP...
5
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which...
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my...
2
by: swhite76 | last post by:
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...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
14
by: cj | last post by:
VB2003. I need a large positive integer. Which is larger int64 or double? I see int64 also apparently is known as long and will hold...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of...
3
by: =?Utf-8?B?RmFiaW8=?= | last post by:
Hi Looking at the following code ... double a = 100.1; double b = 0.1; double c = a + b; I would like to know why the value of c is...
5
by: =?Utf-8?B?anVzdGluc2FyYWNlbm8=?= | last post by:
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.