Consider the following Unit Test:
// Works (sum 0.1 to 0.4)
float f1 = 0.1F + 0.2F + 0.3F + 0.4F;
Assert.AreEqual(1F, f1);
// Works too (sum 0.4 to 0.1)
float f2 = 0.4F + 0.3F + 0.2F + 0.1F;
Assert.AreEqual(1F, f2);
// Works (sum 0.1 to 0.4)
double d1 = 0.1D + 0.2D + 0.3D + 0.4D;
Assert.AreEqual(1D, d1);
// Fails! (sum 0.4 to 0.1)
double d2 = 0.4D + 0.3D + 0.2D + 0.1D;
Assert.AreEqual(1D, d2);
Everything works as expected for the float type (the sum is 1 in both cases), but when using double, the commutativity of the addition is not honored. Indeed, the sum of the first one is 1, but for the second I get 0.99999999.....
I understand why the result is once 1 and once not (because some numbers cannot be represented without loss of precision in base 2) but this does not explain why it works for float and not for double...
Could someone explain this?