More accurate sum

Built-in "sum" function, as well as add.reduce functions in Numeric/numarray introduce a large error when summing large arrays of like elements. I got relative error of about 1e-9 after summing 10 million doubles between 0 and 1. Function below has error less than 1e-15, doesn't use any additional memory (although it destroys the data array), and also runs asymptotically faster for unlimited precision numbers.


You can see that sum([0.123456789012345]*10000000) will be off by almost a 0.001 from the true answer. The solution above will give exact answer for this array. I also got absolute errors less than 1e-15 when trying it on arrays of random numbers. For unlimited precision numbers it also works asymptotically faster. More precisely regular sum takes O(n(d+log d)) while function above takes O(nd) where n is number of elements in array, d is number of digits in each element.

On my machine the function above takes 28% faster for adding 10,000 Decimals than built-in sum. For longs, it takes 10 times longer, presumably because the built-in is optimized for longs

import math

def sum_with_partials(arr):
  size = len(arr)
  iters = math.ceil(math.log(size)/math.log(2))
  for itr in range(int(iters)):
    step = 2**itr
    for i in xrange(0, size, 2**(itr+1)):
      next_i = i+step
      if next_i<size:
        arr[i]+=arr[next_i]
  return arr[0]

code.activestate.com ↗