The first circle from hell. 0.1! = 0.3 / 3

Possible duplicate:
Numerical comparison error in R

Hello everybody,

According to "R Inferno" . I'm in the first round of R hell. This is where pagans expect 0.1 == 0.3 / 3. The document recommends using the function all.equalfor such cases, however I need to check the conditions "> =" or "<=". In the current example, their failure:

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

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

Thank,

Ilya

+5
source share
4 answers

See the following questions:

Generally speaking, you can handle this by including the tolerance level according to the second link above.

+7

all.equal abs(x-y) < tolerance x y tolerance. :

x <= y:         x-y < tolerance
x < y:          x-y < -tolerance
x >= y:         x-y > -tolerance
x > y:          x-y > tolerance
+8

Please see the R FAQ. Why R does not consider these numbers equal and references to them.

+4
source

You can try smart use zapsmall()that seems to give the behavior you are looking for. I do not know if this works in all situations. eg.

.1 >= zapsmall(.3/3)
[1] TRUE
> .1 <= zapsmall(.3/3)
[1] TRUE
+2
source

All Articles