Wednesday, March 12, 2014

Go to Basic/ .NET Floating Numbers

One can surprise why d1 does not equal d2 in the next example

double d1 = 0.6 - 0.2;
double d2 = 0.4;
Assert.AreNotEqual(d1, d2); // d1 != d2

This is a normal behaviour :)

However there are some funny bugs in .NET (imho). For example,

Decimal.Convert(1.51m) != Convert.ToInt32(1.51m);

More cases with explanation can be found at my Go to Basic/ .NET Floating Numbers tips and tricks at codeproject.


No comments:

Post a Comment