0
sum(sorted([0.16666666666666666, 0.08333333333333333, 0.08333333333333333, 0.08333333333333333, 0.08333333333333333, 0.16666666666666666, 0.16666666666666666, 0.08333333333333333, 0.08333333333333333]))

gives 0.9999999999999999 as an output while the same list having different ordering of elements like

sum([0.16666666666666666, 0.08333333333333333, 0.08333333333333333, 0.08333333333333333, 0.08333333333333333, 0.16666666666666666, 0.16666666666666666, 0.08333333333333333, 0.08333333333333333])

gives 1 as an output.

Any way if the former output can be rounded off to 1?

The unsorted list was an output of an operation on python2.7 code. After migrating our code to python 3.8, the output of the operation changed to sorted list and that affected the sum output.

saylee
  • 187
  • 1
  • 2
  • 12

1 Answers1

3

You can use math.fsum(iterable) function.it returns an accurate floating point sum of values in the iterable.

Eanthmue
  • 471
  • 2
  • 5