math.pow(a,n) versus ** operator

Kirby Urner urner at alumni.princeton.edu
Mon Oct 16 02:04:10 EDT 2000


math.pow is clearly more powerful, not barfing 
when exponent is a negative number:

  >>> math.pow(2,-1)  # math.pow(a,-n) == math.pow(1.0/a,n)
  0.5
  >>> 2**-1
  Traceback (innermost last):
  File "<pyshell#297>", line 1, in ?
    2**-1
  ValueError: integer to the negative power

However, ** also seems just plain wrong when raising a 
negative to the 0th power:

  >>> -1**0           # this is wrong
  -1 
  >>> math.pow(-1,0)  # this is correct
  1.0

Any non-zero number to the 0th power is supposed to be 1.  
I can see no justification for this behavior of ** in 
returning -1 for -1**0.

Explanations?

Kirby





More information about the Python-list mailing list