Skip to content
Prev 42046 / 63435 Next

bug in sum() on integer vector

Following this thread, I wondered why nobody tried cumsum to see where the integer
overflow occurs. On the shorter xx vector in the little script below I get a message:

Warning message:
Integer overflow in 'cumsum'; use 'cumsum(as.numeric(.))'
But sum() does not give such a warning, which I believe is the point of contention. Since
cumsum() does manage to give such a warning, and show where the overflow occurs, should
sum() not be able to do so? For the record, I don't class the non-zero answer as an error
in itself. I regard the failure to warn as the issue.

For info, on my Ubnuntu Lucid 10.04 system that has 4 GB of RAM but no swap, the last line
of the script to do the int64 sum chugs for about 2 minutes then gives "Killed" and
returns to the terminal prompt. It also seems to render some other applications unstable
(I had Thunderbird running to read R-devel, and this started to behave strangely after the
crash, and I had to reboot.) I'm copying Romain as package maintainer, and I'll be happy
to try to work off-list to figure out how to avoid the "Killed" result. (On a 16GB
machine, I got the 0 answer.)

Best,

John Nash

Here's the system info and small script.
## sumerr.R  20111214
library(int64)
x <- c(rep(1800000003L, 10000000), -rep(1200000002L, 15000000))
xx <- c(rep(1800000003L, 1000), -rep(1200000002L, 1500))
sum(x)
sum(as.double(x))
sum(xx)
sum(as.double(xx))
cumsum(xx)
cumsum(as.int64(xx))

tmp<-readline("Now try the VERY SLOW int64")
sum(as.int64(x))