Mathematics in Python are not correct

Mark Dickinson dickinsm at gmail.com
Sun May 11 23:26:09 EDT 2008


On May 11, 9:36 pm, "Terry Reedy" <tjre... at udel.edu> wrote:
> Do you have in mind any situations in which it is advantageous to have 0**0
> undefined?

(Playing devil's advocate here.) If you regard x**y as exp(y*log(x))
then it's not at all clear that 0.**0. should be considered
well-defined. And since Python likes integer and floating-point
operations to match numerically (as far as possible), 0**0 then
also comes into question.

The big problem here is that the power operation is really trying
to combine two subtly different functionalities (integer powers
and real powers), with quite distinct use-cases, into a single
function.  Which leads to problems:  witness the mess that's
C99's pow specification:  why does it make sense for (-2.0)**2.0 to
return 4.0, while (-2.0)**1.999999999 returns NaN?  And why
should (-2.0)**infinity be well-defined?

It looks like that same mess is going to make it into IEEE 754r.
There were suggestions on the 754r mailing list to separate
the pow functionality out into two separate functions: pown
for integer powers and powr for real powers.  pown(0.0, 0) would
be 1, while powr(x, y) would be defined strictly as exp(y*log(x))
and so powr(0.,0.) would raise the invalid signal.

I have to admit that almost all my uses of ** are with
integer exponents, and I'd be very upset if anyone took
0**0 == 1 away...

Incidentally, the decimal module is slightly schizophrenic about
this:  Decimal(0)**Decimal(0) raises an exception, but
Decimal('Inf')**Decimal(0) returns Decimal(1).  Which doesn't
make a lot of sense, since every reason for making 0**0
undefined seems to apply equally well to infinity**0...

Mark




More information about the Python-list mailing list