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 exponentiation behaving oddly in python shell
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.1
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: CWardUSC, loewis
Priority: normal Keywords:

Created on 2010-04-09 05:57 by CWardUSC, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg102681 - (view) Author: Chris Ward (CWardUSC) Date: 2010-04-09 05:57
When using exponentiation interactively in the python shell, it returns all negative results when a negative number is the input. For example:

-4 ** 2 will return -16
-4 ** 2 should evaluate as -4 * -4, which correctly returns 16

This does not occur when using the 'Run Module' feature of IDLE and the exponentiation is processed from the module, it only seems to occur when directly typed into the interactive prompt. I couldn't find anything to suggest this is expected behavior. Using pow() from the prompt returns the correct result, so it only happens in this one situation. Obviously this is low priority since it only affects the prompt and there's a working alternative, but I figured I'd report it anyways. :)
msg102682 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-04-09 06:16
This is expected behavior. The power operator binds with higher precedence than the unary minus:

http://docs.python.org/reference/expressions.html#unary-arithmetic-and-bitwise-operations

Therefore, your term is interpreted as

-(4 ** 2)

As for "it only affects the prompt": I can't reproduce that. If I put

print(-4**2)

in a script and run that, it still prints -16 for me.
msg102684 - (view) Author: Chris Ward (CWardUSC) Date: 2010-04-09 06:38
Thanks for clearing that up and pointing me in the right direction. I should have tested print first. The assumption was based on the evaluation of (-4) ** 2 within an expression, which does return correctly. I hadn't made the distinction that the parentheses made it evaluate differently by containing the unary.
History
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52600
2010-04-09 06:38:57CWardUSCsetmessages: + msg102684
2010-04-09 06:16:02loewissetstatus: open -> closed

nosy: + loewis
messages: + msg102682

resolution: not a bug
stage: resolved
2010-04-09 05:57:24CWardUSCcreate