Top-rated recipes tagged "comparisons"http://code.activestate.com/recipes/tags/comparisons/top/2010-03-17T17:05:51-07:00ActiveState Code RecipesApproximately Equal (Python)
2010-03-17T17:05:51-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577124-approximately-equal/
<p style="color: grey">
Python
recipe 577124
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/comparisons/">comparisons</a>, <a href="/recipes/tags/distance/">distance</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/numeric/">numeric</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Generic "approximately equal" function for any object type, with customisable error tolerance.</p>
<p>When called with float arguments, approx_equal(x, y[, tol[, rel]) compares x and y numerically, and returns True if y is within either absolute error tol or relative error rel of x, otherwise return False. The function defaults to sensible default values for tol and rel.</p>
<p>For any other pair of objects, approx_equal() looks for a method __approx_equal__ and, if found, calls it with arbitrary optional arguments. This allows types to define their own concept of "close enough".</p>