Addition of Doubles in C# application producing strange results [closed]

Ultimately, this is because some base-10 numbers (like 0.9) cannot be represented in base 2 in a finite number of digits, just like the fraction 1/3 cannot be represented in base 10 in a finite number of digits (you get 0.333…).

0.9 converted to base 2 is 0.1110011001100…, and this binary representation gets truncated such that the result is as number close to 0.9 (base 10) but is slightly smaller.

Note that you will not see this behavior with the number 0.9, as the float will have a coefficient of 9 and a mantissa of -1. But you will see it with 31.9, as it will have a coefficient of 3.19 (in base 2 = 11.00>11000010100011110101<, repeating section between the >< marks) and a mantissa of +1.

Leave a Comment