[R] "sum" bug?

Bill Simpson W.Simpson at gcal.ac.uk
Fri Mar 22 09:52:55 CET 2002

```I sent a similar message to R-help a few months ago with the same subject!

The basic problem is that sum doesn't automatically coerce to double,
which suprised both you and me. Thus you got integer overflow (doubles
have a much larger dynamic range so won't overflow).

If you had done sum(dep*1.0) that would
have worked as well because operations like * and / do return a double
result, and sum() on a double returns a double.

> x<-1:10
> sum(x)
[1] 55
> is.double(sum(x))
[1] FALSE
> is.double(sum(x*1))
[1] TRUE
> is.double(sum(x+0))
[1] TRUE

It's confusing to me how numbers are stored as integer:
> is.integer(x)
[1] TRUE
> is.integer(x+0)
[1] FALSE
> is.integer(x+x)
[1] TRUE
> y<-0
> is.integer(y)
[1] FALSE
> is.integer(x+y)
[1] FALSE
> y<-0:0
> is.integer(y)
[1] TRUE
> is.integer(x+y)
[1] TRUE

Bill

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```