CKTIS / Python
math.isnan
Devuelve un booleano que indica si el argumento es NaN.
Versión: Python 3.14; repository image, production runtime not attested
Sintaxis
math.isnan(x, /)Devuelve un booleano que indica si el argumento es NaN.
Convertir el texto "nan" a float crea un NaN, de ahí True. El infinito positivo pertenece a otra categoría y da False.
Una comparación mediante == ni siquiera detectaría ese mismo 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)
Ejemplos
import math
print(math.isnan(float("nan")))
print(math.isnan(math.inf))
Resultado
True False
Una comparación mediante == ni siquiera detectaría ese mismo NaN.