This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: X to the power of 0 may give wrong answer
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ncoghlan, tim.peters
Priority: normal Keywords:

Created on 2004-09-10 13:53 by ncoghlan, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg22423 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2004-09-10 13:53
According to Python 2.3 and 2.4, -1 ** 0 = -1 ! It
appears to do this for any negative value.

The answer to x ** 0 should be 1, regardless of the
sign of x.
msg22424 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2004-09-10 14:00
Logged In: YES 
user_id=1038590

The plot thickens. . . if I use the operator module, it works:

Python 2.3.4 (#1, Jun 15 2004, 21:38:43)
[GCC 3.4.0 20040613 (Red Hat Linux 3.4.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> -4.3 ** 0
-1.0
>>> from operator import __pow__
>>> __pow__(-1, 0)
1
>>> -1 ** 0
-1
>>>
msg22425 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-09-10 14:07
Logged In: YES 
user_id=31435

Precedence.  From the language ref:  "The power operator 
binds more tightly than unary operators on its left; it binds 
less tightly than unary operators on its right.".

Python doesn't have negative numeric literals.  -1 is the 
unary minus operator applied to the positive literal 1.  So

-1**0

parses as

-(1**0)

and -1 is the correct result.  Add parens if you want a 
different evaluation order:

>>> (-1)**0
1
>>>
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40897
2004-09-10 13:53:24ncoghlancreate