A range of integers that can be exactly expressed as float / doubles

What is the exact range of (adjacent) integers that can be expressed as double (respectively float?). The reason I am asking is that I am interested, questions such as this one , when there will be a loss of accuracy.

it

  • What is the smallest positive integer msuch that m+1it cannot be exactly expressed as a double (float, respectively)?
  • What is the largest negative integer -nsuch that -n-1it cannot be exactly expressed as double (resp float)? (May be the same as above).

This means that every integer is between -nand mhas an exact floating point representation. I am basically looking for a range [-n, m]for both floats and doublings.

Limit the scope of the standard IEEE 754 32-bit and 64-bit floating point representations. I know that a float has 24 precision bits, and a double has 53 bits (both with a hidden leading bit), but because of the intricacies of the floating point representation, I am looking for an authoritative answer for this. Please do not wave your hands!

(An ideal answer will prove that all integers from 0to mare expressible and that is m+1not.)

+5
source share
1 answer

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

, float 2 ^ 24. 2 ^ 53. , .

+6

All Articles