[Python-ideas] math.inf and math.nan constants

Steven D'Aprano steve at pearwood.info
Tue Jan 13 02:49:13 CET 2015

On Mon, Jan 12, 2015 at 01:40:20PM -0600, Zachary Ware wrote:
> On Mon, Jan 12, 2015 at 1:33 PM, MRAB <python at mrabarnett.plus.com> wrote:
> > I was going to say that calling it "isnan" is in keeping with
> > "isdigit", etc, but those are instance methods!
> >
> > So, why is it "math.isnan(x)" and not "x.isnan()"?
> I'm a little curious about this myself, since float.is_nan(),
> float.is_inf(), and float.is_finite() are all implemented, but #if 0'd
> out.

Can we re-start this issue?


I have a lot of code that takes either a decimal or a float, and I 
currently have to write things like:

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

which is just ick.

If you're wondering why I don't just unconditionally call math.isnan, 
consider this:

py> from decimal import Decimal
py> x = Decimal('snan')
py> float(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot convert signaling NaN to float


More information about the Python-ideas mailing list