This was a comment to a now deleted answer:
The integer 28218681
can be written in binary as 1101011101001010100111001
. Note that 25 digits are needed. Single precision has only 24 bits for its "mantissa" (including the implicit leading 1 bit). 24 is less than 25. Precision is lost. A single-precision representation "remembers" a number only by its leading 24 binary digits. That corresponds to roughly 7-8 decimal figures. Roughly. The integer 28218681
has just 8 figures, so the problem arises.
The lesson learned is, use a type that is "wide" enough to give the desired precision. For example a double
precision number can hold the first ~16 decimal figures of a number.
This is not related to the discussion on whether to use a binary or a decimal format. Note that if the asker had used decimal32
instead of binary32
(another name for float
), he would have had the exact same issue!