[issue47121] math.isfinite() can raise exception when called on a number

Steven D'Aprano report at bugs.python.org
Tue Apr 5 22:51:00 EDT 2022


Steven D'Aprano <steve at pearwood.info> added the comment:

Isn't this just a quality of implementation issue?

math.isfinite should return True for all ints, since all ints are finite. (There are no int infinities or NANs). There is no need to coerce them to float to know that they are finite.

Likewise for Fractions. If they overflow, that could be caught and True returned.

Decimal infinities convert to float infinities, so the only way you can get an overflow error is if the Decimal is finite but too big to convert. So again, isfinite could (should?) catch the overflow error and return True.

Any numeric type that has an infinity which does not coerce to float infinity, but overflows instead, is buggy, and its not isfinite's responsibility to protect against that.

So here is my suggestion:

isfinite() should return True for all ints, without needing to coerce them to float. For other numeric types, it should try to coerce them to float, and catch OverflowError and raise True. This should be documented, so that other numeric types know what contract they are required to follow (infinity coerces to float infinity).

I'm going to change this to an enhancement for 3.11 (or 3.12).

----------
nosy: +steven.daprano
type:  -> enhancement
versions: +Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47121>
_______________________________________


More information about the Python-bugs-list mailing list