Python Forum

Full Version: Exponentiation order of operations
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
G'day everyone

I'm just starting with Python, and already I'm confused about the order of operations for exponentiation. With division, the following evaluates left-to-right as you would expect:

6 / 3 / 2
Output:
1.0
(to produce 1.0, the operation must be (6 / 3) / 2). That's all perfectly reasonable.

The equivalent for exponentiation:
6 ** 3 ** 2
Output:
10077696
I was expecting this to give me (6 ** 3) ** 2 = 46656. It actually produces 10077696 (6 ** (3 ** 2)), implying a right-to-left evaluation order.

Is there any more detailed documentation of how this is evaluated? The order-of-operations documents tend to show how the operations are ordered relative to each other (eg. "**" comes before "/") but not how they're ordered relative to themselves. I had assumed that for operations of the same type, the ordering was simply left to right (as it is for division and subtraction) - but that does not seem to be the case.

Any advice and/or links to documentation would be much appreciated.

Cheers,
Slatye
Here are the docs for Python 2.x, I doubt they've changed: https://docs.python.org/2/reference/expr...precedence
If you write down 6**3**2 on a paper you wull see that you have to do 3**2 first then 6 ** 9 which is the right way. Finally, you  get 10077696
Depends on who you ask. By convention and some languages (which includes Python, Ruby, etc.) evaluation of stacked exponents is from the top down, i.e. a**(b**c), while others (for example LibreOffice, Excel, calculators and I believe Matlab) evaluate from the bottom up, i.e. (a**b)**c.

So actually both ways are correct and depends on the expected output. To be safe and cover your bases, it would probably be best to specify with parenthesis which method you are using.
ichabod801 - thanks for that, I should have realised that this sort of behaviour was unlikely to have changed between 2.x and 3.x. The 2.x documentation will be very useful.

wavic - it seems to be a fairly "loose" convention even in normal maths. I'm a bit surprised that Python actually pays attention to that convention, when maintaining a simple left-to-right would probably be easier.

sparkz_alot - yes, I just checked it with Matlab, and got 46656. I will definitely use parenthesis for this in the future (in both languages). I'm coming from a C background where the problem never arose, because pow(x,pow(y,z)) makes it absolutely clear which order is required.


Is there a way I can mark this thread as "solved"?
We don't really marked threads solved, people generally just stop replying. We have had threads considered closed where someone will respond even after several months.
(Oct-09-2017, 11:35 PM)sparkz_alot Wrote: [ -> ]someone will respond even after several months.

I do that.  :)
We show up a lot on google when people search for very specific things, so having more details added later to help anyone who stumbles upon the thread is always good, imo