I am writing code to parse mathematical expression strings, and noticed that the order in which chained power operators are evaluated in Python differs from the order in Excel.

From http://docs.python.org/reference/expressions.html:

"Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): -1*2 results in -1."*

This means that, in Python: 2**2**3 is evaluated as 2**(2**3) = 2**8 = 256

In Excel, it works the other way around: 2^2^3 is evaluated as (2^2)^3 = 4^3 = 64

I now have to choose an implementation for my own parser. The Excel order is easier to implement, as it mirrors the evaluation order of multiplication.

I asked some people around the office what their gut feel was for the evaluation of 2^2^3 and got mixed responses.

Does anybody know of any good reasons or conciderations in favour of the Python implementation? And if you don't have an answer, please comment with the result you get from gut feel - 64 or 256?

  • It's not that much harder to implement. Anyway, your parser will have to support right associativity in some way or another for things like assignation. Oct 12, 2012 at 11:57
  • Yeah it's not, I actually got it implemented. But I reverted to the Excel convention, because one of my library's primary uses is to export equations to Excel, so it makes more sense to stick to the way the equations work there. Oct 12, 2012 at 11:59

2 Answers 2


The reason why in mathematics stacked exponents are applies from the top down is that the other way you just get multiplication of exponents:

(((2^3)^4)^5) = 2^(3 * 4 * 5)
  • It's not clear form your answer....but isn't that how exponents work in that context? ((2^3)^4)=8^4=(2^3)*(2^3)*(2^3)*(2^3)=2^(3+3+3+3)=2^12 Oct 12, 2012 at 13:38
  • 5
    Yes it is. My point is that of the two orders that you can choose, one yields something that you can write without stacking up exponents. So the interesting associativity is the other one.
    – Andrea
    Oct 12, 2012 at 13:46

Wikipedia (and my math teacher) tells me: Stacked exponents are applied from the top down.

This is reflected the way Python evaluates it. Microsoft is wrong (once more)

And Ruby evaluates it as Python, so it's correct without doubt, since Matz can't be wrong.

  • 4
    See also this interesting post regarding the D Language implementation of the operator which supports right associativity. Oct 12, 2012 at 10:05
  • I recall Visual Basic evaluating it in the same way but I'm not sure whether this adds credibility to this method. ;)
    – Xion
    Oct 13, 2012 at 12:45
  • Microsoft is "wrong" only if it fails to comply to some specification that it claims to follow. Different languages evaluate mathematical operations differently. APL, if I recall correctly, made all operations right-associative. Inconsistency with a different specification is annoying, but not necessarily "wrong". Oct 13, 2012 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.