Python math.isnan() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.isnan(x) returns True if x is an IEEE 754 Not a Number (nan) and False otherwise.

Because IEEE 754 defines nan == nan as False, you cannot detect NaN using == or in reliably on floats—math.isnan() is the standard, readable test.

math.isnan() is a function in Python's standard math module: import it with import math and call math.isnan(...), or import the name directly with from math import isnan.

Syntax

Import math and pass a numeric value.

python
import math

is_missing = math.isnan(x)
CallWhat it does
math.isnan(float("nan"))Returns True.
math.isnan(math.nan)Returns True.
math.isnan(float("inf"))Returns False.
math.isnan(0.0)Returns False.

Beware Python container quirks: inside a list like [float("nan")], object identity makes nan_obj in lst succeed while float("nan") in lst returns False; always filter with math.isnan().

Function signatures
python
math.isnan(x, /)
# returns: bool True if x is IEEE 754 NaN

Parameters

ParameterDescription
xRequired positional real number (int or float). Strings like "nan" raise TypeError.

Return Value

Return ValueDescription
boolTrue if x is Not a Number, else False.

Python Examples

Example 1 — See Why x == float("nan") Fails While math.isnan() Works

Contrast broken == equality checks against math.isnan() and verify inf is not NaN.

python
import math

bad = float("nan")
print("bad == bad   :", bad == bad)
print("bad != bad   :", bad != bad)
print("math.isnan   :", math.isnan(bad))
print("isnan(inf)   :", math.isnan(math.inf))

How It Works

  1. Self-equality fails. By IEEE 754 design, NaN compares unequal to everything, including itself.
  2. Explicit predicate is clearer. Writing math.isnan(bad) states intent far better than bad != bad.
  3. Infinities return False. Positive and negative infinity are not NaN.

Example 2 — Clean Missing NaN Readings Before Averaging

Strip NaN markers out of a float dataset so math.fsum() produces a valid average.

python
import math

temps = [21.5, math.nan, 22.0, math.nan, 24.5]
valid = [t for t in temps if not math.isnan(t)]
avg = math.fsum(valid) / len(valid)

print("valid count:", len(valid))
print(f"average: {avg:.2f}")

How It Works

  1. NaN poisons totals. Summing the unfiltered list would return nan immediately.
  2. Filter with not isnan. A comprehension keeps the three real temperatures and skips both NaNs.
  3. Clean mean computed. Averaging with math.fsum() gives 22.67.

Example 3 — Watch NaN Propagate Through Arithmetic Without Raising

Observe how arithmetic operations and math functions propagate NaN silently.

python
import math

val = math.nan + 5.0
root = math.sqrt(math.nan)
print(math.isnan(val), math.isnan(root))
print(math.isnan(0.0))

How It Works

  1. Contagious across arithmetic. Adding 5.0 to NaN still yields NaN rather than raising.
  2. Functions propagate NaN. Calling math.sqrt(math.nan) returns NaN cleanly instead of throwing ValueError.
  3. Normal zero is False. Ordinary numbers never report NaN.

Frequently Asked Questions

By the IEEE 754 floating-point standard, NaN represents an invalid or unrepresentable result and is never equal to any value, including itself.
No. Strings raise TypeError. Convert with float(s) first before calling math.isnan().
No. Infinities return False for math.isnan() and True for math.isinf().
Yes, operations like float("inf") - float("inf") or math.fsum([math.inf, -math.inf]) produce nan, and math functions given nan return nan.
Use cmath.isnan(z), which returns True if either the real or imaginary part is NaN.

Next: math.isnormal()

Return True if x is a finite, nonzero normal float.

isnormal() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful