Re: Losing precision when converting to double

Efran Cobisi <[email protected]> Mon, 30 Oct 2006 17:29:54 +0100
Newsgroups gmane.comp.windows.devel.dotnet.cx
Organization QBGROUP SpA
Message-ID <[email protected]>
You are right, thanks for the correction.
Actually, by the way, my example should have been:

---
double x = 0;

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

Console.WriteLine(x == 1.0);
---

This one should point out how that... Summing up 0.1 ten times does not
equal 1.

Efran Cobisi, cobisi.com

Sebastien Lorion wrote:
> While you are right, this particular example does not point out precision
> loss but rather inexact conversion from float to double... Removing the f in
> 0.1f or declaring x as a float "fix" the problem.
>
> Sébastien
>
>
>> -----Original Message-----
>> From: Discussion relating to the specifics of the C# and Managed C++
>> languages [mailto:[email protected]] On Behalf Of Efran
>> Cobisi
>> Sent: Monday, October 30, 2006 9:16 PM
>> To: [email protected]
>> Subject: Re: [DOTNET-CX] Losing precision when converting to double
>>
>> 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
>>
>
> ===================================
> 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