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)
Call
What 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
Parameter
Description
x
Required positional real number (int or float).
Return Value
Return Value
Description
bool
True 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)}")
Large finite is not infinite. Although 1e+308 sits near the upper limit, it remains finite and returns False.
Both signs return True. Both math.inf and -math.inf trigger True.
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)}")
Output
inf -> positive inf
-inf -> negative inf
nan -> not a number
42.0 -> finite
How It Works
Sign disambiguation. Once math.isinf(val) returns True, checking val > 0 separates positive from negative infinity.
Check NaN separately.math.isnan() catches values that are neither infinite nor finite.
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")
Output
inf True
int too large to convert to float
How It Works
Arithmetic saturates to inf. Standard Python float multiplication overflows silently to inf, which math.isinf() detects.
Huge int raises instead. Converting an arbitrary-precision integer past float range raises OverflowError.
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.