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: Negative number raised to even power is negative (-1 ** even = negative)
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, rdhunkins
Priority: normal Keywords:

Created on 2017-09-19 14:51 by rdhunkins, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg302529 - (view) Author: Bob Hunkins (rdhunkins) Date: 2017-09-19 14:51
Hello,
I'm only a novice with Python, so please take it easy on me if I have transgressed.

I am using Python 3.6.2 on a win10 64 bit machine, and entered this:

>>> a= [-1 ** x for x in range(1,10)]
>>> print(a)
[-1, -1, -1, -1, -1, -1, -1, -1, -1]
which is not correct.

however, entering this:

>>> b = [pow(-1, x) for x in range(1,10)]
>>> print(b)
[-1, 1, -1, 1, -1, 1, -1, 1, -1]

which is correct.
Any negative number entered and raised to an even power seems to not return a positive value, but the pow(x,y) function works.

I never have reported a bug on any programming language ever, and surprisingly I can't find a report of this (I did search, so if it's out there, I must not understand how to use the search. Forgive me. As I said, I'm very new to Python and I'm not a professional programmer.)
msg302530 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-19 14:57
You are being tripped up by operator precedence:

>>> -1**2
-1
>>> (-1)**2
1
msg302532 - (view) Author: Bob Hunkins (rdhunkins) Date: 2017-09-19 15:09
Thanks. Stupid of me. Appreciate the correction.
msg302533 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-19 15:13
It's a FAQ, but it was faster for me to just cut and paste than it was to look up the FAQ link :)
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75700
2017-09-19 15:13:21r.david.murraysetmessages: + msg302533
2017-09-19 15:09:02rdhunkinssetmessages: + msg302532
2017-09-19 14:57:50r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg302530

resolution: not a bug
stage: resolved
2017-09-19 14:51:10rdhunkinscreate