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: Inconsistent squaring behavior
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: JohnSmith11132, christian.heimes, mark.dickinson
Priority: normal Keywords:

Created on 2020-11-21 19:56 by JohnSmith11132, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg381573 - (view) Author: (JohnSmith11132) Date: 2020-11-21 19:56
Basically when squaring as a negative number, if it's a variable it acts like -3*-3 otherwise it acts like 3*-3.
For example:
>>> x = -3
>>> x ** 2
9
>>> -3 ** 2
-9

pow(-3, 2) seems to act like -3 * -3?
** 3 seems fine and both seems to act like -3*-3*-3.
>>> x = -3
>>> x ** 3
27
>>> -3 ** 3
-27

Will it be safe to use the ** 2 in Python? If I use a variable will it return the same value?

I only found this for the bug:
https://devforum.roblox.com/t/inconsistent-squaring-behavior/820028/

It looks like the exact same issue I'm having, but I still don't get it and it's not for Python.
msg381574 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-11-21 20:04
-3 ** 2 is parsed by Python as -(3 **2), not as (-3) ** 2.
msg381575 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2020-11-21 20:12
Operator precedence and unary operator binding is explained in the language reference documentation, https://docs.python.org/3/reference/expressions.html#the-power-operator
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86594
2020-11-21 20:12:48christian.heimessetstatus: open -> closed

nosy: + christian.heimes
messages: + msg381575

resolution: not a bug
stage: resolved
2020-11-21 20:04:10mark.dickinsonsetnosy: + mark.dickinson
messages: + msg381574
2020-11-21 19:56:52JohnSmith11132create