Feature suggestion: sum() ought to use a compensated summation algorithm

Raymond Hettinger python at rcn.com
Mon May 5 04:08:15 EDT 2008


On May 3, 9:50 am, Szabolcs Horvát <szhor... at gmail.com> wrote:
> I did the following calculation:  Generated a list of a million random
> numbers between 0 and 1, constructed a new list by subtracting the mean
> value from each number, and then calculated the mean again.
>
> The result should be 0, but of course it will differ from 0 slightly
> because of rounding errors.

See:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090



> Here's the program (pardon my style, I'm a newbie/occasional user):
 . . .
> mean = sum(data)/len(data)
> print sum(x - mean for x in data)/len(data)

See:
http://svn.python.org/view/sandbox/trunk/statistics/statistics.py?rev=40981&view=markup


Raymond




More information about the Python-list mailing list