-2

Possible Duplicate:
Why are these numbers not equal?

Can anyone explain to me, why R answers FALSE in the following case?

(1-0.9)>=0.1

How can I get TRUE for comparisons of that kind?

Community
  • 1
  • 1
AndreasG
  • 7
  • 2

3 Answers3

5

This is the classic R FAQ 7.31. You need all.equal

Romain Francois
  • 17,432
  • 3
  • 51
  • 77
5

This has to do with floating point precision. There is in essence an infinite amount of floating points, do representing them in the computer can only be done discretely, and thus with limited precision. To take this limited precision into account, use all.equal to make the comparison. As @RomainFracois said, this is very frequently asked question in R.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
3

You could create your own binary operators to do what you're after and store them in your .Rprofile:

`%>=%` <- function(x, y) all.equal(x, y) | x > y
`%<=%` <- function(x, y) all.equal(x, y) | x < y

c(1-.9)>=.1
c(1-.9)%>=% .1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519