Mathematics in Python are not correct

Terry Reedy tjreedy at udel.edu
Fri May 9 15:35:12 EDT 2008


"Lou Pecora" <pecora at anvil.nrl.navy.mil> wrote in message 
news:pecora-DFE713.11234209052008 at ra.nrl.navy.mil...
| In article <mailman.825.1210293599.12834.python-list at python.org>,
| "Terry Reedy" <tjreedy at udel.edu> wrote:
|
| > "Luis Zarrabeitia" <kyrie at uh.cu> wrote in message
| > news:200805081914.06459.kyrie at uh.cu...
| > | Btw, there seems to be a math problem in python with 
exponentiation...
| > | >>> 0**0
| > | 1
| > | That 0^0 should be a nan or exception, I guess, but not 1.
| >
| > a**b is 1 multiplied by a, b times.  1 multiplied by 0 no times is 1.
| > But there are unenlighted people who agree with you ;-)
| > Wikipedia has a discussion of this.
| >
| > tjr
|
| I like that argument better.  But...
|
| I've also heard the very similar a**b is a multiplied by a b-1 times.

Me too, in school, but *that* definition is incomplete: it excludes b=0 and 
hence a**0 for all a.  It was the best people could do before 0 was known.
But 0 was introduced to Europe just over 800 years ago ;-)

In general, sequence reduction work better if the base case is given 
separately rather that defined as the first member of the sequence (which 
excludes empty sequences!).
ab = [a]*b # b a count
a**b  = reduce(int.__mul__,  ab, 1) # better
a**b = reduce(int.__mul__, ab[1:], a) # crashes for b=0

Consider the equivalent pair of definitions for a*b:
a*b = 0 incremented by a, b times = reduce(int.__add__, ab, 0)
a*b = a incremented by a, b-1 times = reduce(int.__add__, ab[1:] a)

Since we have 0, the second, which excludes (crashes on) a*0 ,
is incomplete.

Terry Jan Reedy






More information about the Python-list mailing list