Oct-09-2017, 01:00 AM
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:
The equivalent for exponentiation:
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
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:
1 |
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:
1 |
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