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: -5**4 returns -625 instead of 625
Type: behavior Stage:
Components: Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: gilbe024, serhiy.storchaka
Priority: normal Keywords:

Created on 2015-02-09 22:40 by gilbe024, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg235642 - (view) Author: Justin (gilbe024) Date: 2015-02-09 22:40
C:\Users\Justin>python
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> print(-5**4)
-625
>>>

#note...this should be 625 and not a negative 625
msg235643 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-02-09 22:46
Unary minus has less priority than power operator. -5**4 is equal to -(5**4), not (-5)**4.
msg235694 - (view) Author: Justin (gilbe024) Date: 2015-02-10 16:13
I think there is a misunderstanding of precedence here. If we had an operation were -1 * 5**4, I could fully understand the statement of precedence. However, in the absence of the "-1 *", the -5 becomes a single atom.

For example:
>>> myVal = -5
>>> myVal**4
625
>>> # not -625 because it is treated as a single atom.

The statement -5**4 - 20, should yield the same result and yet, it does not.

>>> -5**4 - 20
-645
>>> 5**4 - 20
605
>>>

Let's take the reverse side of this

take 4**-2, if precedence is to hold the same way, why would this not be 16 instead of the correct answer of 0.0625. In this instance, the unary sign all of a sudden jumped in precedence and became a single atom.

>>> 4**-2
0.0625
msg235697 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-02-10 17:43
No, -5 is not an atom. See The Python Language Reference [1]:

"""Note that numeric literals do not include a sign; a phrase like -1 is actually an expression composed of the unary operator ‘-‘ and the literal 1."""

[1] https://docs.python.org/3/reference/lexical_analysis.html#numeric-literals
msg235698 - (view) Author: Justin (gilbe024) Date: 2015-02-10 17:53
Thank you for the link. 

I can understand the language definition and I can accept it at face value. The confusion I have is that it does not appear to be consistent. My example of -5**4 vs 4**-2 would both have literals with a unary sign. One works as expected, whereas the other one does not. 

Granted that I am new to Python, although well experience in development and programming; I will conceded to those more experienced in Python development that it is as it should be.

Regards,
History
Date User Action Args
2022-04-11 14:58:12adminsetgithub: 67617
2015-02-10 17:53:18gilbe024setmessages: + msg235698
2015-02-10 17:43:15serhiy.storchakasetmessages: + msg235697
2015-02-10 16:13:57gilbe024setmessages: + msg235694
2015-02-09 22:46:42serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg235643

resolution: not a bug
2015-02-09 22:40:25gilbe024create