1

I am now implementing a function that requires the input list lst to be a distribution, which means sum(lst) is equal to 1.

In the code, I use assert statement

assert sum(lst) == 1, "the lst has to be a distribution"

However, as elements in lst will all be floating numbers, I am not sure whether sum(lst) == 1 will be violated for some subtle cases because of machine precision. To be more specific, I guess my question is

  • How == is implemented in the very low level. What is the tolerance for comparing two floating numbers? Does this value vary machine from machine (for example, the set of values returned by sys.float_info)?
  • When is the good practice of using ==. Besides comparing two floating numbers, are there other similar cases that warrant extra attention (for example, comparing strings, objects, etc).
Mr.Robot
  • 349
  • 1
  • 16
  • 1
    When comparing a sum of a bunch of numbers, I'd recommend using alternatives to floats, like Decimal, since that'd have no rounding issues at all, and you don't have to re-invent the wheel. See [here](https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python) for some good alternatives. It also recommends the function `math.isclose` to compare floats within a certain specified tolerance. – Random Davis Dec 28 '20 at 16:12
  • 1
    or math.fsum(lst) on IEEE-754 algorithm – Ευάγγελος Γρηγορόπουλος Dec 28 '20 at 16:16

0 Answers0