A Question on floating-point precision in R

A simple problem in R ....

 1.1 - 1 < 0.1         FALSE            Yay!! Good answer
 2.1 - 1 < 0.1         FALSE            Another good answer
 4.1 - 4 < 0.1         TRUE             What the ... ?????
 5.1 - 5 < 0.1         TRUE             Sigh
      .
      .
      .
 100.1  - 100 < 0.1              TRUE            I give up
 4.00000000001 - 4 < 0.1         FALSE           This is just silly

I know this has to do with precision and how decimals are stored and unfortunately it is such a fundamental issue that I cannot use it to calculate a tolerance on some models being compared. This is some pretty basic number handling to get this wrong..

1 Like

Some documentation on this, R FAQ - "Why doesn’t R think these numbers are equal?"

For what's it's worth, Python 3 has similar handling of floating-point numbers


You might enjoy this talk by R-core member Martin Mächler:

It even features this exact example, basically.

2 Likes

Thanks so much.. this is exactly the information I wanted

Are you sure about this one? I get TRUE and I cannot think of any language which would give FALSE here.

1 Like

You are so right... I meant to say 4.10000000001 - 4 < 0.1 FALSE

You know how it is.. give a decimal, take a decimal..

Fair enough, but that's not one of the incorrect results. :slight_smile:

In general you need to apply a precision tolerance for such comparisons. This tends to apply for most programming languages.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.