How to add floating point numbers together with a small error?

Viewed 126

There is a file and there are 10000 numbers like the following numbers in the file.

7.474571938241547
5.9520387080950865
8.440712641801738
7.233918953859444
5.359040371183379
7.788831268552525
5.605003246355943
5.464119475471641
4.196570126808656

So I need to add them together with a float type, but there is a margin of error when I adding them together in the original order. So when I adding them in ascending order, the sum becomes smaller. When I adding them in descending order, the sum also becomes smaller. what is the best way to minimize the error?

2 Answers
  1. Use doubles

  2. Use Kahan summation

  3. Use a heap to sort also the intermediate results

    remove the two smallest elements (by absolute value)

    insert the sum back in the heap

  4. 0+1+2

How to add floating point numbers together with a small error?

Simple solution with best accuracy: Perform the calculation using arbitrary precision arithmetic. Convert the output to finite precision if desired.

Efficient solution using finite precision arithmetic: There is no one best algorithm for all use cases. A famous algorithm for floating point summation is Kahan summation algorithm, but research on the subject is active.

Related