[Python-ideas] math.inf and math.nan constants
Case Van Horsen
casevh at gmail.com
Tue Jan 13 05:05:44 CET 2015
On Mon, Jan 12, 2015 at 7:02 PM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
>
> On Mon, Jan 12, 2015 at 9:39 PM, Steven D'Aprano <steve at pearwood.info>
> wrote:
>>
>> 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)
>
>
> Something like this is trivial to implement using the singledispatch module,
> but when you are faced with the functions of two arguments such as pow,
> things get hairier. What type should pow(2.5, Decimal(2)) return?
>
I've thought about about the following for pow(x,y), but it would
break too much existing code (i.e. when x and y are integers).
if isinstance(x, float) and isinstance(y, float):
return the float x**y
try:
return x.__pow__(x,y):
except TypeError:
pass
try:
return y.__pow__(x,y)
except TypeError:
pass
return float(x)**float(y)
It would be nice we could distinguish "real" numbers where a real
number uses a radix, a significand, and an exponent to represent a
number. A float uses a radix of 2, a significant of 53 bits, and
limited exponent range. A Decimal uses a radix of 10, a significand of
arbitrary precision, and a (usually) wide exponent range. Both float
and Decimal would be instances of real numbers. Then the following
might work:
if isinstance(x, real) and isinstance(y, real):
try:
return x.__pow__(x,y):
except TypeError:
pass
try:
return y.__pow__(x,y)
except TypeError:
pass
return float(x)**float(y)
casevh
> _______________________________________________
> 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/
More information about the Python-ideas
mailing list