Skip to content

Documentation of pow is incorrect regarding ** #111228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RunDevelopment opened this issue Oct 23, 2023 · 2 comments
Closed

Documentation of pow is incorrect regarding ** #111228

RunDevelopment opened this issue Oct 23, 2023 · 2 comments
Labels
docs Documentation in the Doc dir

Comments

@RunDevelopment
Copy link

Documentation

The documentation of the pow function currently states:

The two-argument form pow(base, exp) is equivalent to using the power operator: base**exp.

This is incorrect for negative base with exponents between 0 and 1 (both exclusive). E.g.:

>>> pow(-9, 0.5)
(1.8369701987210297e-16+3j)
>>> -9 ** 0.5
-3.0

(Tested for Python 3.8.2 and 3.10.6.)

While pow returns a complex value, ** simply returns the negative square root of the absolute value of base.

Note that this is only the difference in behavior that I observed so far. Issue #60200 suggests there might be more.

@RunDevelopment RunDevelopment added the docs Documentation in the Doc dir label Oct 23, 2023
@Eclips4
Copy link
Member

Eclips4 commented Oct 23, 2023

Hello!
There's no bug. ** has a higher precedence than the negation operator -.
So, your expression calculates as -(9 ** 0.5).
FYI: https://docs.python.org/3/reference/expressions.html#operator-precedence

@Eclips4 Eclips4 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2023
@RunDevelopment
Copy link
Author

RunDevelopment commented Oct 23, 2023

I see. Thank you!

Maybe in Python 4, this could be invalid syntax to protect against this footgun. I mean, JS made this invalid syntax.

> -9 ** 0.5
-9 ** 0.5
^^^^^

SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

2 participants