The cmath(complex math) module provides functions for performing complex mathematical operations. The cmath.isclose()
function is used to determine whether the values of two complex numbers are within a certain tolerance of each other i.e Whether they are close to each other with respect to the given tolerances.
math.isclose(a, b, rel_tol= 1e-09, abs_tol = 0)
a |
The first complex number in the comparison |
b |
The second complex number in the comparison |
rel_tol |
The maximum difference for being considered "close", relative to the magnitude of the input values. Default value is 1e-09 |
abs_tol |
The maximum difference for being considered "close", regardless of the magnitude of the input values. Default value is 0. |
The function returns True
if a is close in value to b, and False
otherwise. For the values to be considered close, the difference between them must be smaller than at least one of the tolerances.
Note: NaN is not close to anything, even itself. inf and -inf are only close to themselves.
import cmath
print(cmath.isclose(10+5j, 10+5j))
print(cmath.isclose(2 + 3j, 2 + 3.1j, rel_tol = 0.1))
print(cmath.isclose(10+5j, 10.01+5j))
print(cmath.isclose(1 + 0.5j, 1 + 0.55j, abs_tol = 0.001))