[Python-ideas] math.inf and math.nan constants
Steven D'Aprano
steve at pearwood.info
Tue Jan 13 03:39:11 CET 2015
On Mon, Jan 12, 2015 at 06:12:05PM -0800, Guido van Rossum wrote:
> 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?)
Ideally you would spell it num.sin(), if anyone ever gets around to
writing a sin routine for Decimals that doesn't go through conversion of
floats first. But I'm not asking for Decimal to support every function
and method that floats do. I'm asking that where Decimal and float both
support the same function, that be a method rather than a function for
one and a method for the other.
I'm very glad that Python doesn't *require* object-oriented code for all
things, as Java does, but there are cases where mixing procedural and OO
interfaces is a nuisance. Now that Python has a full numeric tower,
having the same function be a method for some numbers and a function for
others makes it difficult to write type-agnostic code.
As an alternative to float methods, perhaps the math module could be
more polymorphic, so we could write things like (say):
math.sqrt(some_decimal)
and get a Decimal result instead of a float. I don't particularly care
whether I call a function or a method, so long as I don't have to call
isinstance() first.
Steve
> 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)
More information about the Python-ideas
mailing list