Python 3.2+mathterm
math.isfinite
checking
Returns True if the number is finite — neither inf nor nan. Handy for checking the result of a calculation.
Syntax
math.isfinite(x)
One call covers both anomalies at once: isfinite is false for inf and for nan alike, whereas isinf(nan) is False, so an infinity-only check quietly lets nan through. That matters at the data boundary: float() happily parses the strings 'inf' and 'nan' from input, and overflow in float arithmetic (1e308 * 10) silently yields inf instead of raising.
Examples
import math
print(math.isfinite(42.0)) # → True
print(math.isfinite(math.inf)) # → False
print(math.isfinite(math.nan)) # → False
print(math.isfinite(1e308)) # → True
print(math.isfinite(float('inf'))) # → False
print(math.isfinite(-0.0)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy