r - 什么是 R 中的整数溢出,它是如何发生的?

标签 r integer overflow numeric

我正在进行一些计算并收到以下警告(即不是错误):

Warning messages:
1: In sum(myvar, na.rm = T) :
Integer overflow - use sum(as.numeric(.))

在这个 thread人们说整数溢出根本不会发生。要么 R 不是太现代,要么它们是不正确的。但是,我应该在这里做什么?如果我使用 as.numeric正如警告所暗示的那样,我可能无法解释以前信息丢失的事实。 myvar是从 .csv 文件中读取的,所以 R 不应该弄清楚需要一些更大的字段吗?它是否已经切断了某些东西?
integer 的最大长度是多少?或 numeric ?您会建议任何其他字段类型/模式吗?

编辑:我跑:

R 版本 2.13.2 (2011-09-30)
平台:R Studio 中的 x86_64-apple-darwin9.8.0/x86_64(64 位)

最佳答案

简而言之,integer是范围有限的精确类型,numeric是一种浮点类型,可以表示更广泛的值但不精确。有关详细信息,请参阅帮助页面(?integer?numeric)。

至于溢出,这里是 explanation布赖恩·D·里普利(Brian D. Ripley):

It means that you are taking the mean [in your case, the sum -- @aix] of some very large integers, and the calculation is overflowing. It is just a warning.

This will not happen in the next release of R.



您可以通过给它后缀 L 来指定一个数字是一个整数。 ,例如 1L是整数 1,而不是 1这是一个浮点数,类 "numeric" .

您可以在机器上创建的最大整数由 .Machine$integer.max 给出。 .
> .Machine$integer.max
[1] 2147483647
> class(.Machine$integer.max)
[1] "integer"

添加一个正整数会导致溢出,返回 NA .
> .Machine$integer.max + 1L
[1] NA
Warning message:
In .Machine$integer.max + 1L : NAs produced by integer overflow
> class(.Machine$integer.max + 1L)
[1] "integer"

您可以通过添加浮点值来绕过此限制。
> .Machine$integer.max + 1
[1] 2147483648
> class(.Machine$integer.max + 1)
[1] "numeric"

因为在您的情况下,警告是由 sum 发出的。 ,这表明当数字相加时发生溢出。建议的解决方法 sum(as.numeric(.))应该做的伎俩。

关于r - 什么是 R 中的整数溢出,它是如何发生的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8804779/

相关文章:

python - 如何将多个整数列表转换为单个整数?

css - 为什么这些 div 之间有垂直间隙?

html - CSS 溢出的属性

java - BigInteger 会溢出吗?

html - R Shiny : How to insert html link in a material_button

r - 什么时候以及如何在data.table中使用as.name()vs.get()(例如在循环列中)?

r - $R_LIBS_USER下如何快速复制/更新本地库?

c++ - sprintf 精确类型转换导致编译警告

r - 如何提取多个 zip 文件并在 R 中读取这些 csv?

C++奇怪的输出将字符串转换为int