Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / Python

math.isnan

Renvoie un booléen indiquant si l’argument est NaN.

Version : Python 3.14; repository image, production runtime not attested

Syntaxe

math.isnan(x, /)

Renvoie un booléen indiquant si l’argument est NaN.

Convertir le texte "nan" en float crée un NaN, d’où True. L’infini positif appartient à une autre catégorie et donne False.

Une comparaison par == ne détecterait même pas cette même valeur 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)

Exemples

import math
print(math.isnan(float("nan")))
print(math.isnan(math.inf))

Résultat

True
False

Une comparaison par == ne détecterait même pas cette même valeur NaN.

Sources