While handling a floating point error, I discovered the pyth math moduel's isclose
method. This method calculates a relative epsilon between a
and b
using the following formula:
max(rel_tol * max(abs(a), abs(b))
By default, rel_tol
is set to 1e-09
. According to the documentation, this "assures that the two values are the same within about 9 decimal digits."
Lets consider the following example...
a=1234.000001
b=1234
# the relative epsilon, as calculated with the above formula, would be 0.000001234000001
# so math.isclose returns true
math.isclose(a,b)
Per the documentation, this seems like it should be false
instead, since it's not accurate within the 9th decimal place. But as the relative epsilon is slightly greaer than 1e-06
, which is the difference, it returns true
. I've seen this relative epsilon formula in a few places now, but never with an explanation as to why it is used. I'm hoping to understand why this formula is acceptable, and how I can apply that to my usage of isclose
. Thank you