开发者

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

开发者 https://www.devze.com 2023-01-23 08:00 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Numeric comparison difficulty in R
This question already has answers here: Closed 12 years ago.

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 >=开发者_JAVA技巧 .3/3
[1] TRUE
> .1 <= .3/3
[1] FALSE

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

Thank you,

Ilya


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


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.


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


You could try judicious use of zapsmall() which seems to give the behavior you are looking for. I don't know if this works in all situations. e.g.,

.1 >= zapsmall(.3/3)
[1] TRUE
> .1 <= zapsmall(.3/3)
[1] TRUE
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号