[R] cumsum vs. sum
macrakis at alum.mit.edu
Tue Feb 17 23:12:39 CET 2009
I recently traced a bug of mine to the fact that cumsum(s)[length(s)]
is not always exactly equal to sum(s).
sum(x) - cumsum(x) => 2.8e-17
Floating-point addition is of course not exact, and in particular is
not associative, so there are various possible reasons for this.
Perhaps sum uses clever summing tricks to get more accurate results?
In some quick experiments, it does seem to get more accurate results
It might be worth documenting.
More information about the R-help