Beware: 2 is not always 2 in R

This post is minimalistic.
Consider this:

x <- 2.0000001

Now let's have look at what's inside x:

> [1] 2

But is it really true?

> [1] FALSE

Here you go.
A colleague of mine was once ruined by this for an entire day before we realized what was going on.
So, how to find out what REALLY is the value of x?

> [1] "2.0000001"

10 Responses

  1. Henrik
    Henrik May 15, 2013 at 7:03 am |

    ...alternatively, have a look at ?options / digits.

  2. Hansi
    Hansi May 15, 2013 at 10:08 am |

    Agree, check your output settings. Also don't compare floating point numbers directly.

  3. John
    John May 15, 2013 at 10:53 am |

    Your technique won't work for the most common cases where the additional value is much smaller than the one you've added. If x <- 2 + 2e-15 then as.character() doesn't help at all and it still doesn't equal 2.

  4. Karl Ove Hufthammer
    Karl Ove Hufthammer May 15, 2013 at 2:12 pm |

    Try this:

    x = .1 + .2
    as.character(.3) # ".3"
    as.character(x) # ".3"

    Yet the two numbers are not identical:

    x == .3 # FALSE

    To see why, try:

    formatC(x, digits=50)
    formatC(.3, digits=50)

    To properly compare the two numbers, round them to the required number of decimal places before comparing, e.g., round(x, 1) == .3. The function all.equal() may also be useful.

  5. Jonathan
    Jonathan May 15, 2013 at 6:20 pm |

    Exactly - don't compare floating points, and if you want to see what the difference is between x and 2., just ask:

    x <- 2.0000001
    x # 2
    x-2 # 1e-07

  6. Carl Witthoft
    Carl Witthoft May 15, 2013 at 6:53 pm |

    You do the R language a disservice by publishing this title. In *any* computing language or tool, you'll find both machine precision error limits and a difference between displayed digits and the stored value.

    1. Fr.
      Fr. May 18, 2013 at 1:15 pm |

      I see what you mean but believe the opposite: the "issue" documented here is actually a good example to learn and teach useful knowledge about how computers handle mathematics.

  7. Timothy Sweetser
    Timothy Sweetser May 15, 2013 at 10:05 pm |

    I am always nervous about floating-point equality, but am amazed at how often it works in R.


Leave a Reply