[Python-Dev] sum()

Raymond Hettinger python at rcn.com
Fri Mar 11 23:52:45 CET 2005

> > FWIW, when accuracy is an issue, I use:
> >
> >    sum(sorted(data, key=abs))
> ...and you may still lose a LOT of accuracy that way:-(.
> Raymond, you technically reviewed the 2nd ed Cookbook -- don't you
> recall the sidebar about why partials are the RIGHT way to do
> summations?  I was SO proud of that one, thought I'd made the issue
> clearest than anywhere previously in print...

No doubt about it.  Partials are superior.  My little snippet meets my
needs with no fuss -- my usual situation is many small values whose
accuracy gets clobbered upon encountering a handful of large values.

In the draft statistics module, nondist/sandbox/statistics/statistics.py
I used yet another approach and tracked a separate error term.  The
advantage is adding precision while still keeping O(n) summation speed.

Of course, we can always use the decimal module to add as much precision
as needed.


More information about the Python-Dev mailing list