C- Floating point precision

Binary floating point cannot represent the value 0.1 exactly, because its binary expansion does not have a finite number of digits (in exactly the same way that the decimal expansion of 1/7 does not).

The binary expansion of 0.1 is

0.000110011001100110011001100...

When truncated to IEEE-754 single precision, this is approximately 0.100000001490116119 in decimal. This means that each time you add the “nearly 0.1” value to your variable, you accumulate a small error – so the final value is slightly higher than 1.0.

Leave a Comment