[R] Numbers that look equal, should be equal, but if() doesn'tsee as equal (repost with code included)

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Wed May 28 18:03:03 CEST 2003

Simon Fear wrote:
> Try the following function (the name is supposed to be a joke, by the
> way),
> which will also do the right thing with NAs and characters. Use it as
> if(equal.enough(x,y)) rather than if(x==y), e.g.
>>equal.enough(0.1+0.2, 0.3)

  Oh, why not just go one step further, and redefine the == operator!

  "==" <- function(x,y){equal.enough(x,y)}

 > (.1+.2)==.3

 > (.1+.2)==.3
[1] TRUE

  This requires a slight modification to equal.enough, which I will not 
list here, so that people dont _actually_ do this. It just ensures that 
equal.enough doesn't go all infinitely recursive on us.

  Next time on useless R tips: 1 + 1 = 3


More information about the R-help mailing list