哪个是IEEE 754浮点数不能精确表示的第一个整数? [英] Which is the first integer that an IEEE 754 float is incapable of representing exactly?

查看:235
本文介绍了哪个是IEEE 754浮点数不能精确表示的第一个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  float f0 = 0为了清楚起见,如果我使用的是实现IEE 754浮点数的语言, 。F; 
float f1 = 1.f;

...然后打印出来,我会得到0.0000和1.0000。 / p>

但是IEEE 754并不能代表真实线路上的所有数字。接近于零,差距很小;因此,我的问题是:对于IEEE 754浮点数,这是第一个(最接近于零)的浮点数,整数不能被精确表示?我现在只关心32位浮点数,尽管如果有人给它的话我会有兴趣听到64位的答案!



我认为这将如计算2 of_mantissa 并加1一样简单,其中 of_mantissa 是标准暴露的位数。我为我的机器上的32位浮点数(MSVC ++,Win64)做了这个工作,看起来不错。尾数比特+ 1 + 1

指数中的+1(尾数比特+ 1)是因为如果尾数包含 abcdef ... 它代表的数字实际上是 1.abcdef ...×2 ^ e ,提供了一个额外的隐含位精度。

对于 float ,它是16,777,217(2 24 + / + 1)。

对于 double ,它是9,007,199,254,740,993(2 53 + 1)。


 >>> 9007199254740993.0 
9007199254740992


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.

For float, it is 16,777,217 (224 + 1).
For double, it is 9,007,199,254,740,993 (253 + 1).

>>> 9007199254740993.0
9007199254740992

这篇关于哪个是IEEE 754浮点数不能精确表示的第一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆