CKTIS / Python
math.isnan
Tests whether a numeric argument is a floating-point NaN.
Version: Python 3.14; repository image, production runtime not attested
Syntax
math.isnan(x, /)Tests whether a numeric argument is a floating-point NaN.
Infinity and NaN require separate tests when classifying invalid measurements.
Parsing the text "nan" as float creates a NaN, so the predicate returns True. Positive infinity represents an unbounded magnitude instead and returns False. The Boolean predicate avoids relying on equality, which fails even for the same NaN.
import math
x = float("nan")
assert x != x and math.isnan(x)
assert not math.isnan(math.inf) and not math.isnan(0.0)
Examples
import math
print(math.isnan(float("nan")))
print(math.isnan(math.inf))
Result
True False
Parsing the text "nan" as float creates a NaN, so the predicate returns True. Positive infinity represents an unbounded magnitude instead and returns False. The Boolean predicate avoids relying on equality, which fails even for the same NaN.