Python math.isinf() Function

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

Definition

math.isinf(x) returns True if x is either positive infinity (math.inf) or negative infinity (-math.inf), and False otherwise.

Note that float("nan") is not infinite, so math.isinf(float("nan")) returns False. For the opposite check (neither infinity nor NaN), use math.isfinite().

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

Syntax

Import math and pass a real number.

python
import math

is_infinite = math.isinf(x)
CallWhat it does
math.isinf(float("inf"))Returns True.
math.isinf(float("-inf"))Returns True.
math.isinf(1e308)Returns False (large, but finite).
math.isinf(float("nan"))Returns False.

math.isinf() returns True for both signs of infinity; test x > 0 or x < 0 afterwards if sign matters.

Function signatures
python
math.isinf(x, /)
# returns: bool True if x == +inf or x == -inf

Parameters

ParameterDescription
xRequired positional real number (int or float).

Return Value

Return ValueDescription
boolTrue for +inf or -inf, else False.

Python Examples

Example 1 — Check Finite, Infinite, and NaN Values With math.isinf()

Test ordinary floats, extreme finite values, infinities, and NaN.

python
import math

values = [0.0, 1e308, math.inf, -math.inf, math.nan]
for val in values:
    print(f"{val!r:>10} -> isinf={math.isinf(val)}")

How It Works

  1. Large finite is not infinite. Although 1e+308 sits near the upper limit, it remains finite and returns False.
  2. Both signs return True. Both math.inf and -math.inf trigger True.
  3. NaN is distinct. Not a Number is neither finite nor infinite, returning False.

Example 2 — Classify Positive, Negative, and Unbounded Float Limits

Combine math.isinf() with sign comparison to report infinity direction.

python
import math


def describe(val):
    if math.isinf(val):
        return "positive inf" if val > 0 else "negative inf"
    if math.isnan(val):
        return "not a number"
    return "finite"


for item in [math.inf, -math.inf, math.nan, 42.0]:
    print(f"{item!r:>5} -> {describe(item)}")

How It Works

  1. Sign disambiguation. Once math.isinf(val) returns True, checking val > 0 separates positive from negative infinity.
  2. Check NaN separately. math.isnan() catches values that are neither infinite nor finite.
  3. Clean classifier pattern. Avoids accidental equality bugs.

Example 3 — Catch Float Arithmetic Overflow vs Huge Int OverflowError

See 1e308 * 10.0 become inf while math.isinf(10 ** 400) raises OverflowError.

python
import math

over = 1e308 * 10.0
print(over, math.isinf(over))

try:
    math.isinf(10 ** 400)
except OverflowError:
    print("int too large to convert to float")

How It Works

  1. Arithmetic saturates to inf. Standard Python float multiplication overflows silently to inf, which math.isinf() detects.
  2. Huge int raises instead. Converting an arbitrary-precision integer past float range raises OverflowError.
  3. Use math.isfinite for safety. Check math.isfinite() when rejecting both infinities and NaNs.

Frequently Asked Questions

No. math.isinf(float("nan")) returns False.
No, it returns True for both. Check x > 0 or x < 0 if you need the sign.
Because math.isinf() converts its argument to a C double float first, and 10**400 cannot convert to float without raising OverflowError.
Call math.isfinite(x), which returns True only when x is neither infinite nor NaN.
x == math.inf only matches positive infinity and misses -math.inf. Use math.isinf(x) to catch both.

Next: math.isnan()

Return True if x is a NaN (not a number).

isnan() →

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.

15 people found this page helpful