import numpy as np
n = 10**4
x = np.abs(np.random.rand(n).astype(np.float32))
sum0 = np.sum(x)
b = x.astype(np.float64)
sum1 = np.sum(b)
# https://en.wikipedia.org/wiki/Kahan_summation_algorithm
sum2 = x[0]
c = np.single(0.0)
for i in range(1,n):
y = x[i] - c # assume c is zero.
t = sum2 + y # sum2 is big, y is small
c = (t - sum2) - y # c should still be zero--but isn't. Why?
sum2 = t
# large to smallest
d = np.sort(x)
d = d[::-1]
sum3 = d.sum()
# smallest to largest
e = np.sort(x)
sum4 = e.sum()
print "single = %.20g"%sum0
print "double = %.20g"%sum1
print "Kahan = %.20g"%sum2
print "large to small = %.20g"%sum3
print "small to large = %.20g"%sum4
Which of these is going to do well, which poorly?