Friday, March 31, 2017

Don't compare floats

Floating-point numbers are tricky; one of the first things a programmer needs to remember when working with them is to never check floats for equality. (I believe ReSharper warns you if you do that; I don't know if plain Visual Studio does because I never use it without ReSharper.)

If precise representations of decimal numbers is needed, like when manipulating currencies, use decimals instead (if using C#); if a similar primitive type does not exist in your language, write a separate package / module / library to "fake it" by using integer values and scale them down by two or four digits, depending on your needs. (For example, the number 123456 can represent either 1,234.56 or 12.3456, depending on your application.)

Here's a simple code example to show the difference between floats and decimals in C#:

float f1 = 0.1f;
float f2 = f1 * 10.0f;

float f3 = 0.0f;
for (var i = 1; i <= 10; i++)
    f3 += f1;

Console.WriteLine(f2 == f3);

decimal d1 = 0.1m;
decimal d2 = d1 * 10.0m;

decimal d3 = 0.0m;
for (var i = 1; i <= 10; i++)
    d3 += d1;

Console.WriteLine(d2 == d3);

Running this in LinqPad prints out:


(A peculiarity of the decimal type in C# is that it keeps track of the number of decimal places that have been used, hence the 1.00 vs 1.0 in the second part of the results.)

No comments: