sum accuracy

Random832 random832 at fastmail.com
Wed Apr 13 13:05:10 EDT 2016


On Wed, Apr 13, 2016, at 12:51, Robin Becker wrote:
> Does anyone know if sum does anything special to try and improve
> accuracy? My 
> simple tests seem to show it is exactly equivalent to a for loop
> summation.

No, it doesn't. Sum works on any type that can be added (except
strings), it can't make any assumptions about the characteristics of
floating point types. For non-numeric types, the addition operator may
not be semantically commutative or associative.

Look at
http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/
for an example of a more accurate algorithm, but note that, for example,
this algorithm wouldn't work on complex numbers (you'd have to sum the
real and imaginary components separately)



More information about the Python-list mailing list