floating-pointieee-754rounding-error

Is there a double number better than 1.0 for representing the real number 1 + 1e-16?


It is well known floating-point computation is inexact. For example:

In [64]: 1+1e-16
Out[64]: 1.0

In this case, I would like to know if there is a double number that is better than 1.0 for representing the real number 1+1e-16 (being better means closer to the real number)? In other words, I try to figure out whether this inaccuracy is due to the problem itself or due to how this is solved.


Solution

  • The 53-bit significand precision gives from 15 to 17 significant decimal digits precision (2^−53 ≈ 1.11 × 10^−16). [...] 2^0 × (1 + 2^−52) ~= 1.0000000000000002, the smallest number > 1 wikipedia.

    Explored the following 1+e-17 == 1+1e-16 == 1 != 1+e-15 so I think you are right.

    #include <math.h>
    #include <stdio.h>
    
    int main(void) {
        double d[] = {
            1e-15,
            pow(2, -52),
            1e-16,
            1e-17
        };
        for(size_t i = 0; i < sizeof(d) / sizeof(*d); i++) {
            printf("%d: %.20e, %d\n", i, 1 + d[i], (1 + d[i]) == 1.0);
        }
    }
    

    and the output is:

    0: 1.00000000000000111022e+00, 0
    1: 1.00000000000000022204e+00, 0
    2: 1.00000000000000000000e+00, 1
    3: 1.00000000000000000000e+00, 1