R:大数字错误的算术
当R执行简单算术的大数字时,我在R中遇到了一个问题。有人可以解释发生的事情以及如何工作吗?
> a <- 51569
> b <- a + 6924851946518374722
> b
[1] 6924851946518425600 # problem is visible here already
> c <- b - 6924851946518374722
> c
[1] 51200
因此,添加大数字时的错误为469。为什么?
I encountered a problem in R when it's performing simple arithmetic for big numbers incorrectly. Can someone explain what's happening, and how to work around?
> a <- 51569
> b <- a + 6924851946518374722
> b
[1] 6924851946518425600 # problem is visible here already
> c <- b - 6924851946518374722
> c
[1] 51200
So there's an error of 469 when adding the big number. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
R使用双精度浮点来表示值。精度取决于您的计算机。在我的Intel Machine上,带有IEEE双打,我获得了52位精确度:
R uses double-precision floating-point to represent values. The precision depends on your machine. On my Intel machine with IEEE doubles, I get 52 bits of precision:
r有一个包装GMP的软件包,GNU多个精度库:
cran:ran> cran:r GMP手册
正如评论中指出的那样,报价是必要的。
因为否则r将参数转换为双重,在转换为
bigz
之前丢失精度。还请注意,R尚未支持64位整数,例如
R has a package that wraps GMP, the gnu multiple precision library:
CRAN: R gmp manual
As pointed out in a comment, the quotes are necessary.
because otherwise R converts the argument to a double, losing precision before converting to
bigz
.Note also that R does not yet support 64 bit integers, e.g.