Interesting floating-point summation tip [ https://twitter.com/raymondh/status/1301800636866940928 ]:

"Raymond Hettinger @raymondh

#Python floating point ninja tip: Use parentheses to regroup sums to minimize accumulated round-off error.

Instead of:
a + b + c + d + e + f + g + h

Write:
((a + b) + (c + d)) + ((e + f) + (g + h))

Note, the total work is unchanged."

For more on this see [ https://en.wikipedia.org/wiki/Pairwise_summation ]

#FloatingPointArithmetic #Python #Programming

Raymond Hettinger on Twitter

ā€œ#Python floating point ninja tip: Use parentheses to regroup sums to minimize accumulated round-off error. Instead of: a + b + c + d + e + f + g + h Write: ((a + b) + (c + d)) + ((e + f) + (g + h)) Note, the total work is unchanged. https://t.co/jnJuYzay7jā€

Twitter

For more accuracy, consider Kahan Summation...

"In particular, simply summing n numbers in sequence has a worst-case error that grows proportional to n, and a root mean square error that grows as √n for random inputs. With compensated summation, the worst-case error bound is effectively independent of n, so a large number of values can be summed with an error that only depends on the floating-point precision."

https://en.wikipedia.org/wiki/Kahan_summation_algorithm

#computerscience
#compsci
#WilliamKahan

@sohkamyung

Kahan summation algorithm - Wikipedia