Useful for the case where you want to make sure 2 numbers are the same 'up to precision', no need to specify the tolerance:
Find minimum precision of the 2 numbers
Round both of them to minimum precision and compare
def isclose(a,b):
astr=str(a)
aprec=len(astr.split('.')[1]) if '.' in astr else 0
bstr=str(b)
bprec=len(bstr.split('.')[1]) if '.' in bstr else 0
prec=min(aprec,bprec)
return round(a,prec)==round(b,prec)
As written, only works for numbers without the 'e' in their string representation ( meaning 0.9999999999995e-4 < number <= 0.9999999999995e11 )
Example:
>>> isclose(10.0,10.049)
True
>>> isclose(10.0,10.05)
False