[Rd] bug in sum() on integer vector

Murray Stokely murray at stokely.org
Wed Dec 14 01:24:07 CET 2011


FYI, the new int64 package on CRAN gets this right, but is of course
somewhat slower since it is not doing hardware 64-bit arithmetic.

 x <- c(rep(1800000003L, 10000000), -rep(1200000002L, 15000000))
 library(int64)
 sum(as.int64(x))
# [1] 0

             - Murray

2011/12/9 Hervé Pagès <hpages at fhcrc.org>:
> Hi,
>
>  x <- c(rep(1800000003L, 10000000), -rep(1200000002L, 15000000))
>
> This is correct:
>
>  > sum(as.double(x))
>  [1] 0
>
> This is not:
>
>  > sum(x)
>  [1] 4996000
>
> Returning NA (with a warning) would also be acceptable for the latter.
> That would make it consistent with cumsum(x):
>
>  > cumsum(x)[length(x)]
>  [1] NA
>  Warning message:
>  Integer overflow in 'cumsum'; use 'cumsum(as.numeric(.))'
>
> Thanks!
> H.
>
>> sessionInfo()
> R version 2.14.0 (2011-10-31)
> Platform: x86_64-unknown-linux-gnu (64-bit)
>
> locale:
>  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
>  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
>  [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8
>  [7] LC_PAPER=C                 LC_NAME=C
>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> --
> Hervé Pagès
>
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
>
> E-mail: hpages at fhcrc.org
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list