r - R中的模数错误

标签 r modulo

<分区>

Possible Duplicate:
Why are these numbers not equal?

刚刚注意到 R 中的这个错误。我猜这是 0.6 的表示方式,但有人知道到底发生了什么吗?

根据 R:

0.3 %% 0.2 = 0.1
0.4 %% 0.2 = 0
0.5 %% 0.2 = 0.1
**0.6 %% 0.2 = 0.2**
0.7 %% 0.2 = 0.1
0.8 %% 0.2 = 0

这是怎么回事?

最佳答案

除了@joshua Ulrich 的评论

来自 ?'%%'

%% and x %/% y can be used for non-integer y, e.g. 1 %/% 0.2, but the results are subject to representation error and so may be platform-dependent. Because the IEC 60059 representation of 0.2 is a binary fraction slightly larger than 0.2, the answer to 1 %/% 0.2 should be 4 but most platforms give 5.

也类似于我们得到这个的原因

> .1 + .1 + .1 == .3
[1] FALSE

正如@Ben Boker 指出的那样,您可能想要使用类似的东西

> 3:8 %% 2 / 10
[1] 0.1 0.0 0.1 0.0 0.1 0.0

关于r - R中的模数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13614749/

相关文章:

r - 更改日期变量中的行值

r - 为什么 R 中表达式 isTRUE(3) 和 isTRUE(NA) 的计算结果为 false?

python - 链式模运算的最大值

c++ - 如何在 C++ 中有效地找到具有给定余数的产品?

javascript - JavaScript 中的动画与模数

R:根据条件保持行不重复(多列)

r - 在rstudio/knitr文档中导入常见的YAML

c - 循环未按预期运行

rtweet create_token 在需要 TRUE/FALSE 的地方缺少值

方案:余数函数 - 违反合约