pythonnumpydata-science

Why is it that calling standard sum on a numpy array produces a different result than numpy.sum?


Observe in the following code, creating an numpy array and calling the builtin python sum function produces different results than numpy.sum

How is numpy's sum function implemented? And why is the result different?

test = [.1]*10
test = [np.float64(x) for x in test]
test[5]= np.float64(-.9)

d = [np.asarray(test) for x in range(0,60000)]
sum(sum(d))

outputs

np.float64(-1.7473212210461497e-08)

but

np.sum(d)

outputs

np.float64(9.987344284922983e-12)

Solution

  • Numpy uses pairwise summation:https://github.com/numpy/numpy/pull/3685 but python uses reduce summation.

    The answer is only partially related to FP inaccuracy because if I have an array of FP numbers and use the same algorithm to sum them, I should expect the same result if I sum them in the same order.