Feature suggestion: sum() ought to use a compensated summation algorithm
Arnaud Delobelle
arnodel at googlemail.com
Sat May 3 13:40:20 EDT 2008
Szabolcs Horvát <szhorvat at gmail.com> writes:
[...]
> A little research shows that Mathematica uses a "compensated
> summation" algorithm. Indeed, using the algorithm described at
> http://en.wikipedia.org/wiki/Kahan_summation_algorithm
> gives us a result around ~ 10^-17:
>
> def compSum(arr):
> s = 0.0
> c = 0.0
> for x in arr:
> y = x-c
> t = s+y
> c = (t-s) - y
> s = t
> return s
>
> mean = compSum(data)/len(data)
> print compSum(x - mean for x in data)/len(data)
>
>
> I thought that it would be very nice if the built-in sum() function
> used this algorithm by default. Has this been brought up before?
> Would this have any disadvantages (apart from a slight performance
> impact, but Python is a high-level language anyway ...)?
>
> Szabolcs Horvát
sum() works for any sequence of objects with an __add__ method, not
just floats! Your algorithm is specific to floats.
--
Arnaud
More information about the Python-list
mailing list