Search code examples
rfloating-pointcomparison-operators

Numerical issues in R


It is a well known fact that when comparing two real numbers it is always better to work with a small tolerance to avoid numerical issues.

I am looking for an example where when we compare a number x having d number of decimal places returns FALSE for the below expression:

round(x, d) == x

For example if x=0.1234 then d=4. However, this is not a good example as when you evaluate round(x, d) == x returns TRUE. I am looking for a value of x where round(x, d) == x turns to be FALSE.


Solution

  • It's unclear how you define d. Here I assume it's defined based on exact algebra.

    for (i in 1:20) print(1/(8 * 10^i) == round(1/(8 * 10^i), 3 + i))
    sprintf("%.50f", 1/8e20)
    #[1] "0.00000000000000000000124999999999999993158684291616"