Assert two variables are almost equal in python

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

    You can use the new isclose function introduced in Python 3.5

    PEP 485 adds the math.isclose() and cmath.isclose() functions which tell whether two values are approximately equal or “close” to each other. Whether or not two values are considered close is determined according to given absolute and relative tolerances. Relative tolerance is the maximum allowed difference between isclose arguments, relative to the larger absolute value

    import math
    a = 100.0
    b = 102.0
    c = 103.0
    
    assert math.isclose(a,b, rel_tol=0.02)
    assert math.isclose(a,c, rel_tol=0.02)
    
    0 讨论(0)
  • 2021-01-29 13:59

    Simply define a new test:

    def assertNearlyEqual(self,a,b,fraction=0.02,msg=None):
        if abs(a-b) > abs(fraction*a):
            if msg is None:
                self.fail("The given numbers %s and %s are not near each other."%(a,b))
            else:
                fail(msg)
    

    and call it with your two variables:

    self.assertNearlyEqual(earnings_forecast,actual_earning)
    
    0 讨论(0)
  • 2021-01-29 14:03

    Simple approach:

    a, b = sorted(map(float, (a, b)))
    assert a + abs(a)*1.02 >= b
    

    You can use a custom Testcase subclass for use in tests:

    class FooTestCase(TestCase):
        def assertAlmostEqual(a, b):
            a, b = sorted(map(float, (a, b)))
            self.assertTrue(a + abs(a)*1.02 >= b)
    
    0 讨论(0)
  • 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))
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题