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

Guido van Rossum guido at python.org
Tue Jan 13 03:12:05 CET 2015


But what other methods from Decimal would we have to add to float (and to
int, etc.)?

IIRC there are just too many differences between Decimal and float to ever
hope for equivalence. (How do you spell sin() of a Decimal?)

On Mon, Jan 12, 2015 at 5:49 PM, Steven D'Aprano <steve at pearwood.info>
wrote:

> 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?
>
> http://bugs.python.org/issue18842
>
>
> 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()
> else:
>     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
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150112/165c931c/attachment.html>


More information about the Python-ideas mailing list