可以精确表示为浮点数/双精度数的整数范围

And*_*Mao 6 c# java floating-point double integer

什么是(连续的)整数的确切范围,可以表示为double(resp.浮点?)我问的原因是因为我对这样问题感到好奇,因为会发生精度损失.

那是

  1. 什么是最小正整数m,m+1不能精确表示为double(resp.浮点数)?
  2. 什么是最大的负整数-n,-n-1不能精确表示为double(resp.浮点数)?(可能与上面相同).

这意味着,每一个之间的整数-nm具有精确的浮点表示.我基本上都在寻找[-n, m]浮动和双打的范围.

我们将范围限制为标准IEEE 754 32位和64位浮点表示.我知道浮点数有24位精度,双精度数有53位(都带有隐藏的前导位),但由于浮点表示的复杂性,我正在寻找权威的答案.请不要挥手!

(理想的答案将证明,所有的整数从0m的表达,这m+1是没有的.)

Kyu*_*rem 7

由于您询问IEEE浮点类型,因此语言无关紧要.

#include <iostream>
using namespace std;

int main(){

    float f0 = 16777215.; // 2^24 - 1
    float f1 = 16777216.; // 2^24
    float f2 = 16777217.; // 2^24 + 1

    cout << (f0 == f1) << endl;
    cout << (f1 == f2) << endl;

    double d0 = 9007199254740991.; // 2^53 - 1
    double d1 = 9007199254740992.; // 2^53
    double d2 = 9007199254740993.; // 2^53 + 1

    cout << (d0 == d1) << endl;
    cout << (d1 == d2) << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

0
1
0
1
Run Code Online (Sandbox Code Playgroud)

所以浮动的限制是2 ^ 24.而双倍的限制是2 ^ 53.负数是相同的,因为唯一的区别是符号位.