PC Review


Reply
Thread Tools Rate Thread

Adding two doubles gives incorrect result?

 
 
=?Utf-8?B?RmFiaW8=?=
Guest
Posts: n/a
 
      29th Apr 2007
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 100.19999999999999 and not 100.2?

What should I do to correct this.

Thanks.


 
Reply With Quote
 
 
 
 
Fred Mellender
Guest
Posts: n/a
 
      29th Apr 2007
Floating point types (Float, Double) cannot hold exactly certain decimal
numbers. You could use the Decimal type if you need exact decimal
arithmetic, or use a subroutine package (e.g. GMP Bignum Library) if your
needed precision is greater than what Decimal can provide.

Or, you could do your arithmetic in floating point, convert the answer to
Decimal, and then round that to the precision you need.

"Fabio" <(E-Mail Removed)> wrote in message
news:2E0C4D60-D583-4FBD-AF1E-(E-Mail Removed)...
> 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 100.19999999999999 and not
> 100.2?
>
> What should I do to correct this.
>
> Thanks.
>
>



 
Reply With Quote
 
vladislav zalesak
Guest
Posts: n/a
 
      29th Apr 2007
It has to do with the way a PC stores floating point types. if one value
compared to the other is too much smaller, the computer "discards" part
of the smaller number leading to "errors" like this.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      29th Apr 2007
Fabio <(E-Mail Removed)> wrote:
> 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 100.19999999999999 and not 100.2?


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

> What should I do to correct this.


Well, you're not going to get infinite precision with any of the built-
in types, but for arithmetic like the above, the decimal type would
help you.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding doubles causes unexpected result - bug? =?Utf-8?B?anVzdGluc2FyYWNlbm8=?= Microsoft Dot NET 5 12th Jul 2007 07:23 PM
vlookup based on random result returns incorrect result =?Utf-8?B?cmlja2F0?= Microsoft Excel Worksheet Functions 1 6th Dec 2005 01:16 PM
Median result used in formula gives incorrect result =?Utf-8?B?dmxhdGhhbQ==?= Microsoft Excel Worksheet Functions 4 21st Sep 2005 04:26 PM
Adding double values gives incorrect result =?Utf-8?B?c3doaXRlNzZAbWFwaW5mby5jb20=?= Microsoft Dot NET 2 25th Aug 2004 03:05 PM
incorrect results when working with doubles or singles Patrice Microsoft Excel Programming 1 22nd Feb 2004 05:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:57 AM.