Assert two variables are almost equal in python

后端 未结 5 1953
爱一瞬间的悲伤
爱一瞬间的悲伤 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:06

    For those who still use Python 2.x, you could also use numpy.isclose()

    from numpy import isclose as isclose
    a = 100.0
    b = 100.01
    
    print isclose(a,b, atol=0.02)  # True
    

    From the documentation:

    For finite values, isclose uses the following equation to test whether two floating point values are equivalent.

    absolute(a - b) <= (atol + rtol * absolute(b))
    

提交回复
热议问题