Which is the first integer that an IEEE 754 float is incapable of representing exactly?

前端 未结 2 704
难免孤独
难免孤独 2020-11-22 01:57

For clarity, if I\'m using a language that implements IEE 754 floats and I declare:

float f0 = 0.f;
float f1 = 1.f;

...and then print them

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 02:42

    2mantissa bits + 1 + 1

    The +1 in the exponent (mantissa bits + 1) is because, if the mantissa contains abcdef... the number it represents is actually 1.abcdef... × 2^e, providing an extra implicit bit of precision.

    Therefore, the first integer that cannot be accurately represented and will be rounded is:
    For float, 16,777,217 (224 + 1).
    For double, 9,007,199,254,740,993 (253 + 1).

    >>> 9007199254740993.0
    9007199254740992
    

提交回复
热议问题