Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  marco-fiset Oct 12 '12 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. –  Pieter Müller Oct 12 '12 at 11:59
add comment

2 Answers 2

up vote 9 down vote accepted

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)
share|improve this answer
    
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 –  Pureferret Oct 12 '12 at 13:38
2  
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 '12 at 13:46
add comment

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.

share|improve this answer
4  
See also this interesting post regarding the D Language implementation of the operator which supports right associativity. –  Pedro Romano Oct 12 '12 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 '12 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". –  Keith Thompson Oct 13 '12 at 21:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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