Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First circle of R hell. 0.1 != 0.3/3 [duplicate]

Tags:

r

inequalities

Possible Duplicate:
Numeric comparison difficulty in R

Hello All,

According to "R Inferno" paper. I'm right now in the first circle of R hell. This is where pagans expect 0.1 == 0.3/3. Paper recommends using all.equal function for such cases, however I need to check ">=" or "<=" conditions. With current example on of them fail:

> .1 >= .3/3
[1] TRUE
> .1 <= .3/3
[1] FALSE

Is there a similar function to all.equal that checks inequalities?

Thank you,

Ilya

like image 434
ilya Avatar asked Nov 02 '10 15:11

ilya


Video Answer


3 Answers

The main test of all.equal is whether abs(x-y) < tolerance for some values x and y and some small tolerance. Equivalent inequality tests would check:

x <= y:         x-y < tolerance
x < y:          x-y < -tolerance
x >= y:         x-y > -tolerance
x > y:          x-y > tolerance
like image 151
mob Avatar answered Oct 14 '22 07:10

mob


See these questions:

  • In R, what is the difference between these two?
  • Numeric comparison difficulty in R

Generally speaking, you can deal with this by including a tolerance level as per the second link above.

like image 34
Shane Avatar answered Oct 14 '22 06:10

Shane


Please see the R FAQ entry Why doesn't R think these numbers are equal and the references therein.

like image 6
Dirk Eddelbuettel Avatar answered Oct 14 '22 05:10

Dirk Eddelbuettel