R地狱的第一圈。 0.1!= 0.3/3
可能的重复:
R 中的数值比较难度
大家好,
根据 "R Inferno" 论文。我现在正处于 R 地狱的第一圈。这就是异教徒期望 0.1 == 0.3/3 的地方。论文建议在这种情况下使用 all.equal
函数,但是我需要检查“>=”或“<=”条件。当前的示例中,其中一个失败了:
> .1 >= .3/3
[1] TRUE
> .1 <= .3/3
[1] FALSE
是否有与 all.equal 类似的函数来检查不等式?
谢谢你,
伊利亚
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
all.equal
的主要测试是是否abs(xy)
abs(xy)
abs(xy)
abs(xy)
abs(xy) <某些值
以及一些小的x
和y
的公差公差
。等价不等式检验将检查:The main test of
all.equal
is whetherabs(x-y) < tolerance
for some valuesx
andy
and some smalltolerance
. Equivalent inequality tests would check:请参阅这些问题:
一般来说,你可以处理这个通过包含上面第二个链接的容差级别。
See these questions:
Generally speaking, you can deal with this by including a tolerance level as per the second link above.
请参阅 R 常见问题解答条目为什么 R 认为这些数字不相等以及其中的参考文献。
Please see the R FAQ entry Why doesn't R think these numbers are equal and the references therein.
您可以尝试明智地使用
zapsmall()
,这似乎可以提供您正在寻找的行为。我不知道这是否适用于所有情况。例如,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.,