Python Glossary Русский

Python 3.5+mathterm

math.nan

constants

Not a Number — the special float value produced by undefined operations. It is not equal to itself: nan != nan.

Syntax

math.nan

The only reliable test is math.isnan(x): every comparison against nan is false, so a check like x == math.nan can never fire. It is also contagious — any arithmetic involving nan returns nan, and sorting or min/max over a list containing nan gives a meaningless result because the comparisons stop being consistent. One more trap: math.nan in [math.nan] is True, since containers compare by identity first, while float('nan') in [float('nan')] is False.

Examples

import math
print(math.nan)  # → nan
print(math.isnan(math.nan))  # → True
print(math.nan == math.nan)  # → False (!)
print(math.nan + 1)  # → nan
print(float('nan'))  # → nan
print(math.isnan(0.0))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy