Re: Losing precision when converting to double

Efran Cobisi <[email protected]> Mon, 30 Oct 2006 14:15:41 +0100
Newsgroups gmane.comp.windows.devel.dotnet.cx
Organization QBGROUP SpA
Message-ID <[email protected]>
Well, the double (and single) data type is, by definition, precise at
the extent of the precision of the data type itself.
If precision is needed, you should use the decimal data type.

I like very much the example provided in Martin Fowler's book PoEAA
(converted to C#):

---
double x = 0;

for (int i = 0; i < 10; i++)
    x += 0.1f;

Console.WriteLine(x); // This one will not print out what you're
expecting...
---

HTH,

Efran Cobisi, cobisi.com

Mikael Bergforsen wrote:
> Whenever the result of an operation is a double I loose precision; here
> are some examples:
>
> double a = 1111.111111111;  //a = 1111.1111111109999 OK
>
> converting from decimal
> double a = Convert.ToDouble(1111.111111111M); //a = 1111.111083984375 NOT
> OK
>
> result of addition
> double a = 1111.111111111;
> double b = 1111.111111111;
> double c = a + b; //c = 2222.22216796875 NOT OK
> double c = 2222.222222222; // c = 2222.2222222219998 OK
>
> Any idea why this happens?
>
> ===================================
> This list is hosted by DevelopMentor®  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com