# [Rd] Inconsistencies in sum() (PR#1408)

**ripley@stats.ox.ac.uk
**
ripley@stats.ox.ac.uk

*Fri, 22 Mar 2002 09:02:06 +0100 (MET)*

R 1.4.1
sum() tries to keep the result integer, although that is not
currently documented (and I am about to change it).
>* x <- as.integer(2^30)
*>* x
*
1) inconsistent in warning or not:
>* sum(x, x)
*[1] NA
>* sum(c(x, x))
*[1] NA
Warning message:
Integer overflow in sum(.); use sum(as.numeric(.))
2) It's order-dependent:
>* sum(0.0, x, x)
*[1] 2147483648
>* typeof(.Last.value)
*[1] "double"
>* sum(x, x, 0.0)
*[1] NA
>* typeof(.Last.value)
*[1] "double"
I believe that it should calculate in integers only if *all* the
arguments are integers, that is behave like
>* sum(c(x, x, 0.0))
*[1] 2147483648
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._