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: Please warn about a subtle trap
Type: enhancement Stage:
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, gpk
Priority: normal Keywords:

Created on 2007-08-19 10:25 by gpk, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg32660 - (view) Author: Greg Kochanski (gpk) Date: 2007-08-19 10:25
In python, -1**2 is -1.    This will horribly surprise
most mathematicians or C programmers where
unary - binds very tightly.   Such people will expect
-1**2 == 1.

This problem shows up in scientific contexts, especially
Numpy, where an easy way to generate an alternating
string of positive and negative numbers is
-1**numpy.arange(10).  In this example, one expects to produce [1, -1, 1, -1, ...].

So, please put a note in the documentation warning of this problem.  It can lead to subtly wrong computations.

The appropriate place to put the note is in the Python Reference Manual, section 5.4, The Power operator.

Please insert a final line saying:
"Note: since the minus sign is not part of a numeric literal,
powers of negative numeric constants need to be
written with parentheses. Be aware that -1**2 == -(1**2), not (-1)**2."
msg32661 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-08-21 06:07
Thanks, fixed in rev. 57255.
History
Date User Action Args
2022-04-11 14:56:26adminsetgithub: 45326
2007-08-19 10:25:07gpkcreate