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

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

问题描述

为了清楚起见,如果我使用的是实现 IEE 754 浮点数的语言并声明:

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

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

...然后将它们打印出来,我会得到 0.0000 和 1.0000 - 完全正确.

...and then print them back out, I'll get 0.0000 and 1.0000 - exactly.

但是 IEEE 754 不能代表实线中的所有数字.接近于零,差距"很小;距离越远,差距越大.

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.

所以,我的问题是:对于 IEEE 754 浮点数,它是第一个(最接近于零)无法精确表示的整数?我只关心 32 位浮点数现在,尽管如果有人给出 64 位的答案,我会很感兴趣!

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!

我认为这就像计算 2bits_of_mantissa 并加 1 一样简单,其中 bits_of_mantissa 是标准公开的位数.我在我的机器(MSVC++,Win64)上为 32 位浮点数做了这个,不过看起来还不错.

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.

推荐答案

2尾数位 + 1 + 1

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

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.

因此,第一个无法准确表示且将被四舍五入的整数是:
对于 float,16,777,217 (224 + 1).
对于 double,9,007,199,254,740,993 (253 + 1).

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屋!

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