Search code examples
rfloating-pointroundingprecisionieee-754

Extreme numerical values in floating-point precision in R


Can somebody please explain me the following output. I know that it has something to do with floating point precision, but the order of magnitue (difference 1e308) surprises me.

0: high precision

> 1e-324==0
[1] TRUE
> 1e-323==0
[1] FALSE

1: very unprecise

> 1 - 1e-16 == 1
[1] FALSE
> 1 - 1e-17 == 1
[1] TRUE

Solution

  • R uses IEEE 754 double-precision floating-point numbers.

    Floating-point numbers are more dense near zero. This is a result of their being designed to compute accurately (the equivalent of about 16 significant decimal digits, as you have noticed) over a very wide range.

    Perhaps you expected a fixed-point system with uniform absolute precision. In practice fixed-point is either wasteful or the ranges of each intermediate computation must be carefully estimated beforehand, with drastic consequences if they are wrong.

    Positive floating-point numbers look like this, schematically:

    +-+-+-+--+--+--+----+----+----+--------+--------+--------+--
    0
    

    The smallest positive normal double-precision number is 2 to the power of the minimal exponent. Near one, the double-precision floating-point numbers are already spread quite wide apart. There is a distance of 2-53 from one to the number below it, and a distance of 2-52 from one to the number above it.