def _float_approx_equal(x, y, tol=1e-18, rel=1e-7): if tol is rel is None: raise TypeError('cannot specify both absolute and relative errors are None') tests = [] if tol is not None: tests.append(tol) if rel is not None: tests.append(rel*abs(x)) assert tests return abs(x - y) <= max(tests) def approx_equal(x, y, *args, **kwargs): """approx_equal(float1, float2[, tol=1e-18, rel=1e-7]) -> True|False approx_equal(obj1, obj2[, *args, **kwargs]) -> True|False Return True if x and y are approximately equal, otherwise False. If x and y are floats, return True if y is within either absolute error tol or relative error rel of x. You can disable either the absolute or relative check by passing None as tol or rel (but not both). For any other objects, x and y are checked in that order for a method __approx_equal__, and the result of that is returned as a bool. Any optional arguments are passed to the __approx_equal__ method. __approx_equal__ can return NotImplemented to signal that it doesn't know how to perform that specific comparison, in which case the other object is checked instead. If neither object have the method, or both defer by returning NotImplemented, approx_equal falls back on the same numeric comparison used for floats. >>> almost_equal(1.2345678, 1.2345677) True >>> almost_equal(1.234, 1.235) False """ if not (type(x) is type(y) is float): # Skip checking for __approx_equal__ in the common case of two floats. methodname = '__approx_equal__' # Allow the objects to specify what they consider "approximately equal", # giving precedence to x. If either object has the appropriate method, we # pass on any optional arguments untouched. for a,b in ((x, y), (y, x)): try: method = getattr(a, methodname) except AttributeError: continue else: result = method(b, *args, **kwargs) if result is NotImplemented: continue return bool(result) # If we get here without returning, then neither x nor y knows how to do an # approximate equal comparison (or are both floats). Fall back to a numeric # comparison. return _float_approx_equal(x, y, *args, **kwargs)