How best to sum up lots of floating point numbers?

For "more precise": this recipe in the Python Cookbook has summation algorithms which keep the full precision (by keeping track of the subtotals). Code is in Python but even if you don't know Python it's clear enough to adapt to any other language.

All the details are given in this paper.


See also: Kahan summation algorithm It does not require O(n) storage but only O(1).


There are many algorithms, depending on what you want. Usually they require keeping track of the partial sums. If you keep only the the sums x[k+1] - x[k], you get Kahan algorithm. If you keep track of all the partial sums (hence yielding O(n^2) algorithm), you get @dF 's answer.

Note that additionally to your problem, summing numbers of different signs is very problematic.

Now, there are simpler recipes than keeping track of all the partial sums:

  • Sort the numbers before summing, sum all the negatives and the positives independantly. If you have sorted numbers, fine, otherwise you have O(n log n) algorithm. Sum by increasing magnitude.
  • Sum by pairs, then pairs of pairs, etc.

Personal experience shows that you usually don't need fancier things than Kahan's method.