[R] cumsum vs. sum
ggrothendieck at gmail.com
Wed Feb 18 02:53:18 CET 2009
Check out sum.exact and cumsum.exact in the caTools package.
Loading required package: bitops
> x <- 1/(12:14)
> sum(x) - cumsum(x)
> sum.exact(x) - cumsum.exact(x)
On Tue, Feb 17, 2009 at 5:12 PM, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
> I recently traced a bug of mine to the fact that cumsum(s)[length(s)]
> is not always exactly equal to sum(s).
> For example,
> 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
> than cumsum.
> It might be worth documenting.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help