all Python 3cmathfunction
cmath.isnan()
checking
Checks whether at least one part of a complex number is NaN. Comparing with == does not work for this.
Syntax
cmath.isnan(z)
NaN equals nothing, not even itself, so z == cmath.nan and z == cmath.nanj are always False: a check with == silently lets NaN through. For a complex value the function looks at both parts: complex(1, nan) is NaN, although its real part is fine.
Examples
import cmath
z = complex(1, float('nan'))
print(z == z, cmath.isnan(z)) # → False True
print(cmath.nanj == cmath.nanj) # → False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy