[R] cumsum vs. sum

Berwin A Turlach berwin at maths.uwa.edu.au
Wed Feb 18 20:45:07 CET 2009


G'day all,

On Wed, 18 Feb 2009 12:41:27 -0500
Stavros Macrakis <macrakis at alum.mit.edu> wrote:

> Hmm.  Why not use the same method to guarantee the same result?  

Hmm, I did not look at the source code but, potentially, sum() could
use some tricks to reduce rounding errors further that would not be
available to cumsum(); e.g. sorting the data before adding summing
them; or grouping them into groups of roughly similar magnitude and
then sum group-by-group.  So it does may be counter-productive to use
the same method.....

> Or at least document the possibility that cumsum(x)[length(x)] !=
> sum(x)... that seems like an easy trap to fall into.

But this is already documented, isn't it?  FAQ 7.31. ;-))

Cheers,

	Berwin




More information about the R-help mailing list