The math.isclose()
function is used to determine whether two numerical values are close enough to be considered equal given a certain level of precision.
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Parameters
a |
The first value to be compared, it should be a real number. |
b |
The second value to be compare, it should also be a real number |
rel_tol |
An optional parameter that specifies the relative tolerance for the comparison. It defaults to 1e-09 |
abs_tol |
An optional parameter that specifies the absolute tolerance for the comparison, it defaults to 0.0 |
Return Value
This function returns True
if the two values passed in are considered close enough within the given tolerances, and False
otherwise.
Let’s take a look at an example of using custom tolerance levels.
This function can be useful if precision is necessary when working with a large collection values.