[R] Question about summing to zero

From: Jim Frederick <James.Frederick_at_uncp.edu>
Date: Wed 19 Jul 2006 - 05:27:39 EST


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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Jul 19 06:45:41 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 19 Jul 2006 - 08:17:53 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.