How best to sum up lots of floating point numbers?

前端 未结 5 1565
我寻月下人不归
我寻月下人不归 2020-11-28 22:11

Imagine you have a large array of floating point numbers, of all kinds of sizes. What is the most correct way to calculate the sum, with the least error? For example, when t

5条回答
  •  爱一瞬间的悲伤
    2020-11-28 22:37

    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.

提交回复
热议问题