9.6 - 9.4 != 0.2

Mar
24
2009
In Categories: .NET | CLR
Tags | |

I was asking one of my friends Ebeid - who works as a Developer Support Engineer at Microsoft Regional Developer Support Center - about the strangest case he had ever experienced in .NET. The answer was very strange and astonished me; simply stated, the problem was "9.6 - 9.4" is not always equals to 0.2 :). I try it and it gives me 0.199999999999999. I searched about this a lot and I didn't find anything with a good benefit except this forum post at MSDN social network.

Fourm summary:

The reason for this problem is that some floating point numbers cannot be precisely represented in binary, ust like 1/3 cannot be precisely represented in decimal. 
To solve the problem use FormatNumber or formatCurrency functions which allows you do some quick rounding and manipulation of your output.

Take care next time when you subtract two decimal numbers :)
 

Comments

Sherif
Sherif Egypt
2/26/2009 7:49:40 AM Permalink

i like it.. Smile

Heba Khaled
Heba Khaled Egypt
2/26/2009 11:15:18 PM Permalink

this is the first time to know this info Smile
thanks ya Ghoz.

Ebeid Soliman
Ebeid Soliman Egypt
2/28/2009 2:53:19 AM Permalink

Thanks Ghoz for your acknowledgement.
Actually this strange behaviour, lead me to discover more and more strange behaviours. like the following:

* Floating-Point Expressions do not compare as equal, although they are mathematically equal.
* Mode operator does not return accurate result.
* Boolean types does not convert to numeric type accurately
* Character literal generates compiler error
* String conversion fails at run time

All these cenarios and how to solve them are available in "Troubleshooting Data Types" section at msdn.microsoft.com/en-us/library/ae382yt8.aspx

you still think that the data types is the easiest part of any programming language ;)

Ebeid Soliman
Ebeid Soliman Egypt
3/1/2009 7:24:45 AM Permalink

Hi Slim,

just try to run this code:

double d1 = 0.8;
double d2 = 0.2;
double d3 = 0.6;
if (d3 == (d1 - d2))
   MessageBox.Show("C# is accurrate");
else
   MessageBox.Show("C# is inacurrate");

// This how you could overcome this issue
if (d3.ToString() == (d1 - d2).ToString())
   MessageBox.Show("Now, C# is accurrate");
else
   MessageBox.Show("C# is inacurrate");

just run it and tell me what you think Smile

Best Regards,
Ebeid Soliman

Hussein
Hussein Egypt
3/6/2009 4:08:14 PM Permalink


As was told before, this because some decimal numbers cannot be fully reprsented by binary like:

decimal = 0.2
0.2 * 2 = 0.4 --> 0
0.4 * 2 = 0.8 --> 0
0.8 * 2 = 1.6 --> 1
0.6 * 2 = 1.2 --> 1
0.2 * 2
0.00110011001100110011.......

decimal 0.3
0.3 * 2 = 0.6 --> 0
0.6 * 2 = 1.2 --> 1
0.2 * 2 = 0.4 --> 0
0.4 * 2 = 0.8 --> 0
0.0100110011001100110011....

while others can be fully reprsented like:

decimal : 0.25
0.25 * 2 = 0.5 --> 0
0.5 * 2 = 1 --> 1
0.01 exactly

so when a not-fully reprsented number is stored in the machine, and as it will be stored as a binary number whatever the machine is, it may or may not be converted to decimal correctly when we call it back depending on the reprsentation used for storing the binary number(IBM mesh 3aref a, IEEE mesh 3aref kam) along with the algorithm used for rounding back the number to a decimal

sami
sami Egypt
3/10/2009 10:12:50 PM Permalink

wooow i like this one Smile

Mostafa Wanas
Mostafa Wanas Egypt
3/24/2009 10:59:04 AM Permalink

I like that one

I really like the ToString() function more and more

Comments are closed