简体   繁体   中英

Assert two variables are almost equal in python

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

I want to assert if both these variables are equal with a difference of ±2% acceptable with respect to actual_earning variable.

Suppose: earnings_forecast = 6 actual_earnings = 5.19

I cannot use assertEqual(earnings_forecast, actual_earnings) because it will try do an exact match, instead I want to assert both these variables are almost equal with ±2% difference acceptable.

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)
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.

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)

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)

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM