CKTIS / Python
math.isinf
Deteta precisamente os infinitos positivo e negativo.
Versão: Python 3.14; repository image, production runtime not attested
Sintaxe
math.isinf(x, /)Deteta precisamente os infinitos positivo e negativo.
O teste classifica o valor após a conversão para float. Em CPython 3.14 converter 10**400 gera OverflowError e o valor finito Decimal("1e400") passa a infinito positivo. Ambos os valores da lista dão True. NaN também não é finito, mas não é infinito e dá False.
Um número finito, incluindo zero, dá False.
import math
from decimal import Decimal
assert math.isinf(Decimal("1e400")) is True
assert math.isinf(math.inf) and math.isinf(-math.inf)
assert not math.isinf(math.nan) and not math.isfinite(math.nan)
Exemplos
import math
print([math.isinf(x) for x in (math.inf, -math.inf)])
print(math.isinf(math.nan))
Resultado
[True, True] False
Um número finito, incluindo zero, dá False.