<分区>
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
这是怎么回事?
<分区>
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/