Python Forum
Exponentiation order of operations
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exponentiation order of operations
#1
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
Reply
#2
Here are the docs for Python 2.x, I doubt they've changed: https://docs.python.org/2/reference/expr...precedence
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
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"?
Reply
#6
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replicate Excel operations with Python Lumberjack 3 1,773 May-10-2022, 01:44 AM
Last Post: Lumberjack
  Program demonstrates operations of bitwise operators without using bitwise operations ShawnYang 2 1,757 Aug-18-2021, 03:06 PM
Last Post: deanhystad
  Random Choice Operations Souls99 6 2,889 Jul-31-2020, 10:37 PM
Last Post: Souls99
  Create bot to automate operations in IQ Option JonatasCavalini 0 12,271 Jul-19-2020, 02:23 AM
Last Post: JonatasCavalini
  Two operations in two ranges salwa17 3 2,094 Jun-22-2020, 04:15 PM
Last Post: perfringo
  How to avoid slow cursor operations? sevensixtwo 0 1,827 Oct-11-2019, 02:23 PM
Last Post: sevensixtwo
  Operations on indexed variables in loop Fibulavie 1 1,900 Aug-14-2019, 06:07 AM
Last Post: fishhook
  Confused by order of operations ward 4 2,591 Jan-22-2019, 08:53 PM
Last Post: Larz60+
  Beginner user: mathematical operations Mahdi1994 1 2,818 Mar-19-2018, 11:07 AM
Last Post: Larz60+
  Need my program to run concurrent operations cygnus_X1 1 3,101 Jul-21-2017, 10:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020