all Python 3mathterm
math.isnan
checking
Returns True if the number is NaN (Not a Number). It cannot be tested with ==, because nan != nan.
Syntax
math.isnan(x)
nan does not come from division by zero (that raises ZeroDivisionError) but from float('nan'), from parsing the text 'nan' out of input, and from operations like math.inf - math.inf. A separate trap: the in operator and list.index compare by identity first, so the very same nan object is reported as present in a list even though nan == nan is false — the only reliable way to find or filter nan is math.isnan.
Examples
import math
print(math.isnan(math.nan)) # → True
print(math.isnan(float('nan'))) # → True
print(math.isnan(42.0)) # → False
print(math.isnan(math.inf - math.inf)) # → True
print(math.isnan(0.0)) # → False
print(math.isnan(math.nan * 0)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy