2

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
    use doubles... they are twice than floats – ΦXocę 웃 Пepeúpa ツ Feb 01 '21 at 12:50
  • 6
    Floating point arithmetic on computers will always include some rounding errors, which will always compound (and get larger) the more arithmetic operations you do using the results. – Some programmer dude Feb 01 '21 at 12:52
  • 1
    In general, for more accurate results you should add things together from (the absolute value of) smallest to largest. However, that will only help if things are significantly different in size. In your case where things are pretty similar in size that doesn't matter and you'll get general floating point errors whichever way you do it. Either use a larger type (double?) or a larger precision floating point library: https://en.wikipedia.org/wiki/List_of_arbitrary-precision_arithmetic_software – Mike Vine Feb 01 '21 at 12:59
  • Also note that in general a float can only hold about 7 decimal values of accuracy, so as _soon_ as you load those values from a file into a float, you've lost accuracy _even before you've done any adding_. – Mike Vine Feb 01 '21 at 13:01
  • Generally, error is larger when you add quite different numbers. If all your numbers have the same order of magnitude, you may reduce the error by grouping elements, summing by groups, then summing the group sums ... – Damien Feb 01 '21 at 13:02
  • 1
    Does this answer your question? [In which order should floats be added to get the most precise result?](https://stackoverflow.com/questions/6699066/in-which-order-should-floats-be-added-to-get-the-most-precise-result) – acraig5075 Feb 01 '21 at 13:05
  • 3
    If you need lossless accuracy, use some sort of BigNum or BCD that can add numeric strings, rather than `float` or `double`. – Eljay Feb 01 '21 at 13:09
  • Here's an implementation of the [Klein sum algorithm](https://godbolt.org/z/no55Yz). "_a second-order "iterative Kahan–Babuška algorithm"_" as it says on wikipedia. – Ted Lyngmo Feb 01 '21 at 13:41
  • 1
    @Someprogrammerdude: How is that comment helpful? (a) OP knows floating-point typically has rounding errors, so asserting it is not useful. (b) OP does not ask to eliminate rounding errors, the question asks to minimize the error. (c) The statement is not literally true; floating-point software can be written to get exact answers in certain situations. I have done so when, for example, writing test code to manipulate values in particular ways. (d) It is also not true the errors always compound or get larger; sometimes they cancel and even vanish. – Eric Postpischil Feb 01 '21 at 14:30
  • hide on bush, What is the range of value 1.0 - 10.0? or the entire `float` range. + and - values? – chux - Reinstate Monica Feb 02 '21 at 03:51

2 Answers2

2
  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

Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57
1

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326