[R] Question about summing to zero

Jim Frederick James.Frederick at uncp.edu
Tue Jul 18 21:27:39 CEST 2006


Hi,

When I used
x <- c(10,10,10,10,5,5,5,5)
sum(x)
x <- x - .Last.value/8
the result was zero, as I expected.
However, when I used
x <- rnorm(101,0,10)
sum(x)
x <- x - .Last.value/101
sum(x)
I did not get zero, but  -2.664535e-15.  OK, that's fairly close to 
zero, but I tried to improve the figure.  Each time I repeated the last 
two commands, the sum got closer to zero.  The series seemed to converge 
on -1.30e-15, so changed the 101 to 50, then to 20, and then to 10.  I 
had sum(x) down to -5.551115e-17 before I quit.

If R can store values of x which sum to -5.551115e-17, then why can't I 
get that value on the first try?  Is this a bug or just rounding error?

Jim Frederick



More information about the R-help mailing list