How best to sum up lots of floating point numbers?

前端 未结 5 1546
我寻月下人不归
我寻月下人不归 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:22

    Well, if you don't want to sort then you could simply keep the total in a variable with a type of higher precision than the individual values (e.g. use a double to keep the sum of floats, or a "quad" to keep the sum of doubles). This will impose a performance penalty, but it might be less than the cost of sorting.

    0 讨论(0)
  • 2020-11-28 22:25

    If your application relies on numeric processing search for an arbitrary precision arithmetic library, however I don't know if there are Python libraries of this kind. Of course, all depends on how many precision digits you want -- you can achieve good results with standard IEEE floating point if you use it with care.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 22:39

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

    0 讨论(0)
  • 2020-11-28 22:45

    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.

    0 讨论(0)
提交回复
热议问题