Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / Python

math.isfinite

Reconhece números finitos, incluindo os dois zeros com sinal.

Versão: Python 3.14; repository image, production runtime not attested

Sintaxe

math.isfinite(x, /)

Reconhece números finitos, incluindo os dois zeros com sinal.

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. Para 0, infinito positivo e NaN obtêm-se True, False e False.

O zero negativo do segundo teste continua finito e o infinito negativo também é excluído.

import math
from decimal import Decimal
assert math.isfinite(Decimal("1e400")) is False
assert all(math.isfinite(x) for x in (0, -0.0, 1.5))
assert not any(math.isfinite(x) for x in (math.inf, -math.inf, math.nan))

Exemplos

import math
print([math.isfinite(x) for x in (0, math.inf, math.nan)])
print(math.isfinite(-0.0))

Resultado

[True, False, False]
True

O zero negativo do segundo teste continua finito e o infinito negativo também é excluído.

Fontes