Ambiguous PEMDAS

Oliver Knill

April/9/2014



Abstract: even in mathematics, ambiguities can be hard to spot. The phenomenon seen here in arithmetic goes beyond the usual PEMDAS rule and illustrates an ambiguity which can lead to heated arguments and discussions.


What is 2x/3y-1 if x=9 and y=2 ?

Try to answer this question before continuing to read.









Did you get 11 or 2? If you got 11, then you are in the BEMDAS camp, if you got 2, you are in the BEDMAS camp.
In either case you can relax because you have passed the test. If you got something different you are in trouble although! There are arguments for both sides. But first a story.

A true story

I got today following lovely story from the director of curriculum of some some school district:

"The problem 2x/3y-1 with x=9 and y=2 was actually posed for a 5th grade homework question and it is interesting how much debate has occurred between our parents. The students who had help from parents had the answer of 2. The teacher explained that the correct answer was 11."


Who is right?

I wrote back:

"It depends on how the brackets are understood. There are two interpretations:
x=9; y=2;

(2x/3)y-1            gives  11
2x/(3y)-1            gives   2
It is not clear what the textbook had intended with the 3y. As written, it can be interpreted both ways. Yes, one could argue that without brackets the given order matters. One can however also argue that "3y" is a unit which belongs together. So, everybody is right and that the textbook problem has just been unclear."


This is actually a really fun topic. Lets expand a bit on it and explain why we can argue both ways. I bet most humans get the answer 2. [Update April 16, 2014: I made an experiment with my calculus class, in which
60 students submitted answers on paper: all except two got the answer 2.


Interestingly, the two other answers were 18/5. This is what you get if read it as 2x/(3y-1). Nobody got 11]. The interesting thing is that if you want to use technology then the answer is 11:

javascript gives 11, or wolfram alpha gives 11 or mathics (based on python) gives 11 too.

What is x/3x?

It depends again on whether one has 3x bundled together or not. Both answers x2/3 or 1/3 should be counted correct because the parenthesis had been missing. One should write either x/(3x) or (x/3)x in order to make the problem clear. Here is what the computer algebra system Mathematica does:
x/3x         

         2
        x
Out[1]= --
        3

x/(3x)         

        1
Out[2]= -
        3
The computer has chosen to do the division first. This is natural if one looks as division and multiplication as operations on the same level. To play the devils advocate, note however that the famous PEMDAS (parenthesis, exponents, multiplication, division, addition and subtraction) has the M before the D so that we would have to accept the second one ...

Its funny that in Excel, there is a rule BEDMAS (Brackets, Exponents, Division, Multiplication, Addition, Subtraction), which reverses Multiplication and division. One could therefore call the original question the BEMDAS-MEDMAS conflict. Different programming languages might do things differently. Your browser computes in Javascript with the BEDMAS rule:
  
  
gives both 10/3. Try it out. Funny by the way (and totally unrelated to the topic discussed here) that the results are different
3.333333333333333
3.3333333333333335 
illustrating that computers do not honor basic laws of arithmetic. The mathematics of rounding error arithmetic is subtle. My PhD advisor Oscar Lanford knew much about this. By the way, also Mathematica adhers to BEDMAS and does division before multiplication. It is in line with Excel and javascript as probably most programming languages like Perl.

What is the source of the problem?

We know that addition and subtraction orders can be interchanged:
3+4-3  = (3+4)-3 = 3+(4-3)
This is called associativity. Since the logarithm gives an isomorphism between the group (R,+) and (R -{0}, *), where * is multiplication, one could think that we also have the same associativity in the multiplicative case. This is true because the multiplicative group (R-{0},*) is by definition associative. But it is the role of the division which must be made clear:
12/(3*2)      = 2
(12/3)*2      = 8
gives different answers. This can not be discovered well in algebra situations like
12/3x
which is ambiguous. Both of the following expressions are clear, but give different results:
12/(3*x)      = 4/x
(12/3)*x      = 4x
This is also often a source of error in addition, as the minus sign has to be carried through:
3-4+3   
(3-4)+3 = 2
3-(4+3) = -5 
It is this case which makes the argument that the bracket should be placed to the left. The additive case is also a reason which seduces not to write the brackets in the multiplicative case. It always leads to trouble.

An other example

What is
x/x/x     ?
We again have two interpretations
(x/x)/x = 1/x
x/(x/x) = x
But since now, we have only divisions and no multiplication, it is even less clear what comes first. When dealing with fractions, we often make this clear by the size of the division lines
    x
  -----
    x            =  x
    -
    x
It has become clear that we first do the division with the smaller division line and
    x
    -  
    x            =  1/x
   ----
    x
What does Mathematica do? Lets look and enter
2/100/2
2/(100/2)   = 1/25
(2/100)/2   = 1/100
Mathematica choses the second choice (as does javascript). It makes the first division first.

An ambiguous example with exponents

So, Mathematica does things from the left. Lets look now at exponents. What is
3^3^3  ?
Again we have an ambiguity with two cases:
(3^3)^3 = 19683
3^(3^3) = 7625597484987
Leaving out the brackets invites trouble. Yes, one could argue that the first example is more natural, but then, a printed version of a textbook might have the third 3 smaller so that the reader is seduced to first compute 3^3 and put that into the exponent, producing a much larger number. In the following example, this is even less clear what the writer intends because expressions like 102 are often treated together as we write 106 for a million. The 106 has become a unit: so, if we write
2^10^2
which of the two answers
(2^10)^2   = 1048576
2^(10^2)   = 1267650600228229401496703205376
is correct? Maybe we should ask our professional computer algebra system again. Mathematica 9.0 gives the second choice, if 2^10^2 is entered!
Now we see that even professionals have difficulty to decide.

For (a/b/c) the algebra system starts evaluating from the left, while for (a^b^c) it starts evaluating from the right.


How come, Mathematica does that? The reason is that a^x is a short hand for Exp[x Log[a]]. Now, if you write expressions like Exp[Exp[Exp[x]]], then by nature, we start evaluating from the right. If we write f(g(x)), we first compute g(x) and then f(g(x)) even so f is written first.

Lets go back to division. If we write f(x)=1/x then f(f(x))=x but (1/1)/x) = 1/x and 1/(1/x) = x. We see that the argument of starting to evaluate the division from the right when writing 1/1/x has some merit too because it even gives in the division case the result we would get if we wrote the expressions as operators.
This is why the parents and the teacher have disagreed. And both were right. Because both way to write things make sense in some way.

There is only one solution: write the parenthesis.


P.S. This reminds me: when I was in school, there had been a war about whether to use Reverse Polish Notation (RPN) or not for calculators. There was the "HP camp" using calculators from Hewlett-Packard which used RPN and there was the "TI camp" using calculators from Texas Instruments which did not. I had been in the TI camp (even hardware hacking them) and as a student found the RPN strange. Of course, after having studied mathematics and working with computer algebra systems for a long time, RPN has become natural too. In Mathematica, one often makes computations by adding commands from the right like in
256 // Sqrt // Exp // Floor // PrimeQ
which takes the square root of 256, then exponentiates it, take the integer part and then answers the question whether this is prime. It is equivalent to
PrimeQ[Floor[Exp[Sqrt[256]]]]
but avoids the brackets. I use the first way often when computing interactively, while the second one when writing code like the following which looks for the statistics of primes in a subexponential sequence of integers:
F[n_]:=Floor[Exp[Sqrt[n]]]; 
s=Table[If[PrimeQ[F[n]],1,0],{n,1000}];
G[n_]:=Sum[s[[k]],{k,n}]*Log[n]/n;
ListPlot[Table[G[n],{n,Length[s]}],PlotRange->{0,1}]
If you are curious: here is the Plot.

P.S. An other interesting (a bit unrelated) story is the ambiguity introduced by decimal marks and dividing numbers into groups. Especially in journalism, one writes things like 121.123 or 121,123 meaning 121123 and not 121 + 123/1000. Even so it is now well established that "the symbol for the decimal marker shall be either the point on the line or the comma on the line" the comma or decimal point is still used frequently to divide numbers into groups, despite the fact that it clearly violates the ISO 31-0 standard. But habits are hard to change.

Document history: