What is integer overflow in R and how can it happen?

前端 未结 4 1382
粉色の甜心
粉色の甜心 2020-11-28 08:36

I have some calculation going on and get the following warning (i.e. not an error):

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


        
4条回答
  •  [愿得一人]
    2020-11-28 09:13

    In short, integer is an exact type with limited range, and numeric is a floating-point type that can represent a much wider range of value but is inexact. See the help pages (?integer and ?numeric) for further details.

    As to the overflow, here is an explanation by 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.

    You can specify that a number is an integer by giving it the suffix L, for example, 1L is the integer one, as opposed to 1 which is a floating point one, with class "numeric".

    The largest integer that you can create on your machine is given by .Machine$integer.max.

    > .Machine$integer.max
    [1] 2147483647
    > class(.Machine$integer.max)
    [1] "integer"
    

    Adding a positive integer to this causes an overflow, returning 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"
    

    You can get round this limit by adding floating point values instead.

    > .Machine$integer.max + 1
    [1] 2147483648
    > class(.Machine$integer.max + 1)
    [1] "numeric"
    

    Since in your case the warning is issued by sum, this indicates that the overflow happens when the numbers are added together. The suggested workaround sum(as.numeric(.)) should do the trick.

提交回复
热议问题