R 无法舍入 126.5

R fails to round 126.5

R 无法四舍五入数字“126.5”。这是我无意中发现的。

round(125.5) # = 126, correct
round(126.5) # = 126, wrong
round(127.5) # = 128, correct

我预计 round(126.5) 的输出是 127,但实际输出是 126。 R 正确舍入其他数字(见上文)。有人知道问题出在哪里吗?我该如何解决?

来自文档 ?round -

Note that for rounding off a 5, the IEC 60559 standard is expected to be used, ‘go to the even digit’. Therefore round(0.5) is 0 and round(-1.5) is -2. However, this is dependent on OS services and on representation error (since e.g. 0.15 is not represented exactly, the rounding rule applies to the represented number and not to the printed number, and so round(0.15, 1) could be either 0.1 or 0.2).