Assert two variables are almost equal in python

后端 未结 5 1949
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 13:15

Here are two variables: earnings_forecast, actual_earning (numerical variables)

I want to assert if both these variables are equal with a diffe

5条回答
  •  抹茶落季
    2021-01-29 14:13

    abs(earnings_forecast - actual_earning) < 0.01 * abs(earnings_forecast + actual_earning)
    

    is a nice way of doing it, which gives you a good symmetric 2% difference on either side. It also doesn't suffer from pitfalls that can arise of one of the values is zero.

    There are other definitions, but like the one above, they have their own pros and cons.

提交回复
热议问题