本文介绍了R:大数的错误算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当R错误地对大数执行简单的算术运算时,我遇到了一个问题.有人可以解释正在发生的事情以及如何解决吗?

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

所以加大数时有469的错误.为什么?

So there's an error of 469 when adding the big number. Why?

推荐答案

R使用双精度浮点表示值.精度取决于您的机器.在具有IEEE双精度的Intel机器上,我得到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:

> options(digits=22)
> (0:1) + 2^52
[1] 4503599627370496 4503599627370497
> (0:1) + 2^53
[1] 9007199254740992 9007199254740992

这篇关于R:大数的错误算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 20:03