2

Why would the order of the values yield different totals?

data = (
    -81.9672,
     48.3607,
     48.3607,
     40.9836,
    -40.9836,
    -40.9836,
     81.9672,
     81.9672,
    -81.9672,
     40.9836,
    -48.3607,
    -48.3607,
)

sum_order_1 = sum(data)
sum_order_2 = sum(sorted(data))
sum_order_3 = sum(sorted(data,key=lambda x:abs(x)))

print(sum_order_1) # Gives 1.4210854715202004e-14
print(sum_order_2) # Gives 2.842170943040401e-14
print(sum_order_3) # Gives 0.0
Ronnis
  • 12,593
  • 2
  • 32
  • 52
  • 5
    This question is tagged C++ but the answer essentially holds here too https://stackoverflow.com/questions/6699066/in-which-order-should-floats-be-added-to-get-the-most-precise-result tl;dr floating point nonsense – Cory Kramer Feb 02 '23 at 16:14
  • 1
    Suppose you had 2 decimal digits of precision. Then 3+3+3+600 would give 610, but 600+3+3+3 would give 600. – interjay Feb 02 '23 at 16:20
  • 1
    For the particular case of summation, there is `math.fsum`, which produces the same result for all three orders, unless I bungled my test program. And by the way, instead of `lambda x: abs(x)`, just say `abs`. :-) – Ture Pålsson Feb 02 '23 at 18:06
  • @TurePålsson, Thanks for both points. Much appreciated. My actual problem is within a pyspark environment where I do `amount * N / sum(N) over(partition by order_id)` where I end up with random sums all the time due to the parallel nature... but since I could re-create the problem in pure python I thought to ask that instead. Perhaps I'll ask another question with another minimal example. – Ronnis Feb 02 '23 at 18:24

0 Answers0