Numeric comparison difficulty in R

Posted by Matt Parker on Stack Overflow See other posts from Stack Overflow or by Matt Parker
Published on 2010-05-04T22:55:48Z Indexed on 2010/05/04 22:58 UTC
Read the original article Hit count: 319

I'm trying to compare two numbers in R as a part of a if-statement condition:

(a-b) >= 0.5

In this particular instance, a = 0.58 and b = 0.08... and yet (a-b) >= 0.5 is false. I'm aware of the dangers of using == for exact number comparisons, and this seems related:

(a - b) == 0.5) is false, while

all.equal((a - b), 0.5) is true.

The only solution I can think of is to have two conditions: (a-b) > 0.5 | all.equal((a-b), 0.5). This works, but is that really the only solution? Should I just swear off of the = family of comparison operators forever?

© Stack Overflow or respective owner

Related posts about r

    Related posts about comparison