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

时间:2013-03-26 17:00:35

标签: c# java floating-point integer double

(连续)整数的确切范围是什么,可以表示为double(resp。浮点?)我问的原因是因为我对questions such as this one感到好奇,因为会发生精度损失。< / p>

那是

  1. 最小正整数m是什么,m+1不能精确表示为double(resp.float)?
  2. 最大的负整数-n是什么,-n-1无法精确表示为double(resp.float)? (可能与上述相同)。
  3. 这意味着-nm之间的每个整数都有一个精确的浮点表示。我基本上都在为浮动和双打寻找范围[-n, m]

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

    (理想的答案将证明0m的所有整数都是可表达的,而m+1则不是。{/ p>

1 个答案:

答案 0 :(得分: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;
}

输出:

0
1
0
1

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

相关问题