all Python 3mathterm
math.isinf
checking
Returns True if the number is infinite (float('inf') or float('-inf')).
Syntax
math.isinf(x)
Infinity almost never comes from dividing by zero — that raises ZeroDivisionError; the real sources are float('inf'), math.inf, parsing user input, and overflow in float arithmetic such as 1e308 * 10. If you only want to confirm a number is sane, reach for math.isfinite instead: isinf(nan) is False, so an infinity check alone is not enough.
Examples
import math
print(math.isinf(math.inf)) # → True
print(math.isinf(-math.inf)) # → True
print(math.isinf(42.0)) # → False
print(math.isinf(1e309)) # → True (overflow)
print(math.isinf(float('inf'))) # → True
print(math.isinf(0.0)) # → False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy