Home All Groups Group Topic Archive Search About

Problem with simple calculation

Author
22 Aug 2006 9:25 PM
SONET
I am currently using the following

Studio 2005:  8.0.50727.42 (RTM.050727-4200)
..NET Framework: Version 2.0.50727

When doing the following in C#

double a = 11.08;
double b = 11.04;
double c = a - b;

I get  0.0400000000000009 instead of 0.04.  Why is this?

Am i missing something? 

Thanks

Author
22 Aug 2006 9:33 PM
Jon Skeet [C# MVP]
SONET <SO***@discussions.microsoft.com> wrote:
Show quote
> I am currently using the following
>
> Studio 2005:  8.0.50727.42 (RTM.050727-4200)
> .NET Framework: Version 2.0.50727
>
> When doing the following in C#
>
> double a = 11.08;
> double b = 11.04;
> double c = a - b;
>
> I get  0.0400000000000009 instead of 0.04.  Why is this?
>
> Am i missing something? 

Yes - how binary floating point works.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
22 Aug 2006 9:42 PM
Nick Malik [Microsoft]
Yes,  You are missing something.  Double is a floating radix datatype, as
defined by the IEEE 754 standard.  It is useful for scientific calculations.
If you intend to perform simple decimal calculations, I'd suggest that you
use the Decimal datatype.

The C# newsgroup FAQ is here:
http://www.yoda.arachsys.com/csharp/faq/

This particular item is discussed (in significant detail) here:
http://www.yoda.arachsys.com/csharp/floatingpoint.html

--
--- Nick Malik [Microsoft]
    MCSD, CFPS, Certified Scrummaster
    http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
   I do not answer questions on behalf of my employer.  I'm just a
programmer helping programmers.
--
Show quote
"SONET" <SO***@discussions.microsoft.com> wrote in message
news:3A802E39-28E9-4D8C-A369-AB121281B5F4@microsoft.com...
>I am currently using the following
>
> Studio 2005:  8.0.50727.42 (RTM.050727-4200)
> .NET Framework: Version 2.0.50727
>
> When doing the following in C#
>
> double a = 11.08;
> double b = 11.04;
> double c = a - b;
>
> I get  0.0400000000000009 instead of 0.04.  Why is this?
>
> Am i missing something?
>
> Thanks
>

AddThis Social Bookmark Button