If you're saying:>>> z = 1.0 - sum([0.1]*10) >>> z == 0False>>> is_close(0.0, z)Trueyour "reference" value is probably really "1.0" or "0.1" since those are the values you're working with, but neither of those values are derivable from the arguments provided to is_close().
def is_close(a, b=None, tol=1e-8, ref=None):
and get reasonable looking results, I think? (If you want to use an absolute tolerance, you just specify ref=1, tol=abs_tol).
An alternative thought: rather than a single "is_close" function, maybe it would make sense for is_close to always be relative,
If you had a sequence of numbers and wanted to do both relative comparisons (first n significant digits match) and absolute comparisons you'd just have to say:for a in nums:assert is_close(a, b) or is_close_abs(a, b)which doesn't seem that onerous.