哪个是 IEEE 754 浮点数无法准确表示的第一个整数? [英] Which is the first integer that an IEEE 754 float is incapable of representing exactly?
问题描述
为了清楚起见,如果我使用的是实现 IEE 754 浮点数的语言,并且我声明:
float f0 = 0.f;浮动 f1 = 1.f;
...然后将它们打印出来,我会得到 0.0000 和 1.0000 - 正是如此.
但 IEEE 754 无法表示沿实线的所有数字.接近于零,差距"很小;距离越远,差距越大.
所以,我的问题是:对于 IEEE 754 浮点数,它是第一个(最接近于零的)整数,不能精确表示?我只真正关心 32 位浮点数现在,尽管如果有人给出答案,我会很想听听 64 位的答案!
我认为这就像计算 2bits_of_mantissa 并加 1 一样简单,其中 bits_of_mantissa 是标准公开的位数.我在我的机器(MSVC++、Win64)上为 32 位浮点数做了这个,不过看起来还不错.
2尾数位 + 1 + 1
指数中的+1(尾数位+1)是因为,如果尾数包含abcdef...
,它所代表的数字实际上是1.abcdef... × 2^e
,提供额外的隐含精度.
因此,不能被准确表示并且将被四舍五入的第一个整数是:
对于 float
,16,777,217 (224 + 1).
对于 double
,9,007,199,254,740,993 (253 + 1).
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 back out, I'll get 0.0000 and 1.0000 - exactly.
But IEEE 754 isn't capable of representing all the numbers along the real line. Close to zero, the 'gaps' are small; as you get further away, the gaps get larger.
So, my question is: for an IEEE 754 float, which is the first (closest to zero) integer which cannot be exactly represented? I'm only really concerned with 32-bit floats for now, although I'll be interested to hear the answer for 64-bit if someone gives it!
I thought this would be as simple as calculating 2bits_of_mantissa and adding 1, where bits_of_mantissa is how many bits the standard exposes. I did this for 32-bit floats on my machine (MSVC++, Win64), and it seemed fine, though.
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
这篇关于哪个是 IEEE 754 浮点数无法准确表示的第一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!