[New-bugs-announce] [issue18842] Add float.is_finite is_nan is_infinite to match Decimal methods

Steven D'Aprano report at bugs.python.org
Mon Aug 26 17:59:17 CEST 2013


New submission from Steven D'Aprano:

On issue 15544 Mark Dickinson suggested adding methods to float to match methods on Decimal, giving type-agnostic ways of testing real numbers that don't rely on converting to float. I don't see any sign that Mark raised a feature request, so I'm taking the liberty of doing so myself.

Note that the math.is* functions convert to float first, which means that they behave differently. Example: math.isnan(Decimal('sNAN')) raises ValueError, rather than returning True.

float.is_nan
float.is_infinity
float.is_finite

would mirror the spelling of Decimal methods, rather than the math module. As float doesn't support signalling NANs, there probably isn't any need to include is_snan and is_qnan.

For what it's worth, I have code that would use this. I currently write something like:

if isinstance(x, Decimal) and x.is_nan() or math.isnan(x): ...

in order to prevent triggering the ValueError on signalling NANs.

----------
messages: 196219
nosy: stevenjd
priority: normal
severity: normal
status: open
title: Add float.is_finite is_nan is_infinite to match Decimal methods
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18842>
_______________________________________


More information about the New-bugs-announce mailing list