There’s this meme on the internet about two calculators (or phone calculator apps) with different results. This is a common misconception and therefore a perfect topic for my blog.
Usually it’s about the expression 6/2(2+1)
. Note the implied multiplication before the bracket.
This is one example:
You can trust both to give you a correct answer, because 9 and 1 are both correct. Both do exactly what is specified. So READ THE FUCKING MANUAL that you got with the calculator.
There are other articles and even videos explaining the maths behind this. As a programmer you should at least be able to find them using any web search. There you will find explanations on how implied multiplication often (but not always) has higher precedence. This is also known as abbreviated multiplication. The multiplication sign (i.e. *, ×, or ·) is omitted.
But as a programmer you must also understand that this is about parsing expressions. And even more importantly, you must understand that the correct answer is the one that is in coherence with the specifications. And for that you must talk to the clients.
But let’s have a closer look at the expression:
Our expression is this:
6/2(2+1)
That is equivalent to:
a := 2+1
6/2a
Would you really say that this is the same as:
(6/2) * a
Unless it is specified that implied multiplication is always treated the same as explicit multiplication, the juxtaposition 2a
has higher precedence in the expression than the division . You can’t just replace it by 2*a
. You replace it by (2*a)
. It does not matter if it’s a
or (2+1)
, as we defined those to be the same.
Note: Someone disagreed and so I added an appendage at the end.
Here’s an example on how the two interpretations come from how you parse the expression:
“Six divided by twice the sum of two and one. You get one.”
is not the same as
“Six divided by two, multiplied by the sum of two and one. You get nine.”
The wording is different and so are the results. But the expressions in the calculators are the same. So it’s not really about the wording but how it’s parsed.
If implied multiplication is the same as explicit multiplication, then it’s just:
6/2*(2+1) = 3*3 = 9
See the resulting syntax tree, which has the 6/2
and 2+1
subexpressions as a subtree. Therefore the two subresults (both are 3) are put in as the values for the multiplication.
The division (/
) is “higher” in the tree (shown lower because the tree is upside down) than the multiplication, because the expression is parsed left to right.
If implied multiplication has higher precedence (as many would expect) it’s:6/(2*(2+1)) = 6/6 = 1
This syntax tree has /
at the “lowest” level (root of the tree) because implied multiplication (show as *
) has higher precedence. The +
is even higher in the tree because it’s in brackets and the value is used as the multiplicand in the expression. The brackets only influence the structure of the tree, but are not shown here.
Here’s the order of operations (precedence) as they are given in the manual for the calculator shown left in the above image:
As you can see, the abbreviated multiplications (5 and 7) have higher precedence than multiplication (10). That is obviously not the case for the calculator app.
Your job as a programmer is to implement the parser so it does exactly what was specified.
As a user of a calculator you must read the manual to see how it handles implied multiplication.
It’s wrong to say that one answer is correct and the other is not, unless you know the specifications.
A calculator could just as well give you SYNTAX ERROR
as the answer. That’s when the calculator doesn’t support implied multiplication. In that case it can’t create the syntax tree and you get that error.
Arguments for the different results:
- 1: Many expect implied multiplication to take higher precedence. That means expressions like
2√3/9π
don’t have to be written as(2*√3)/(9*π)
. The juxtapositions takes higher precedence and the brackets are not necessary. Anyone not aware of this gets wrong results. - 9: This uses a simpler grammar and anyone who knows BOMDAS or any similar mnemonic, can just use that. All multiplication is handled the same. Not typing the multiplication sign doesn’t change how the expression is parsed. Anyone expecting higher precedence of implied multiplication gets wrong results.
- ERROR: Same as the arguments for 9, but there is no risk of the user expecting another set of rules. This is the only behaviour that prevents false results due to ambiguity.
What people who don’t understand this, do wrong:
- They can’t accept that there are arguments against what they believe and so they stay ignorant.
This is sad and dangerous. They do no research and don’t even think about the problem to see if the other solution could possibly be correct. Instead they just assume that they are right and the others are just trolling or stupid. Like cult members they are trained to refuse any information that conflicts with their preconceived notion of the correct answer. Instead of learning they get angry. - They get their calculator and type in the expression.
This is wrong, because there are two ways and you will just get 9 or 1, but that doesn’t prove that the other result is wrong. It just shows you don’t understand the ambiguity of implied multiplication. - They talk about BOMDAS/PIMDAS/PEMDAS.
That just shows they understand primary school arithmetic. But this is about something that is not handled by those mnemonics. Not one of them says anything about implied multiplication.
It turns out that simplified acronyms taught to kids don’t explain all of mathematics. Grow up! - They claim that in their favourite programming language they get 9 and so it must be correct.
But they don’t use the same expression. Instead they use explicit multiplication, which is not the same. As a programmer you must not ignore such details. - They claim that mathematics are unambiguous.
That’s not true. This is about language used in mathematics. It can be ambiguous in the sense that there are different notations in use. They are all based on the works of Adam Ries, who made mathematics books in the 16th century to be used in schools. But there is no official rule for the precedence of implied multiplication in mathematics. I’m not aware of any standardised language that uses it. Maybe there’s something in ISO/IEC 40314:2016 or IEEE 260.3-1993, but I will not pay hundreds of dollars to read them. They probably just tell you not to use it.
Appendage: Terms vs Groupings
It was pointed out to me here that some authors distinguish between implied multiplication inside of terms and implied multiplication between a group and some other expression.
- This is a term with coefficient 7:
7x²
- This is an expression with implied multiplication:
2(2+1)
The distinction is that one is just a term, which is defined as one or more factors with implied multiplication, while the other one is a value in front of a grouping symbol.
With this clear distinction is is possible to define different precedence for the two different types of multiplication. It allows juxtapositions without having to use brackets and also use implied multiplication with groups but have the same precedence there as with explicit multiplication:
- Parentheses (grouping symbols)
- Exponentiation
- Implied multiplication inside a term (juxtapositions)
- M/D
- Explicit multiplication
- Implied multiplication in front of a grouping symbol
- Division
- A/S
- Addition
- Subtraction
As you can see it is still something that is not handled by PEMDAS. You would have to insert a T for “terms” or a J for “juxtapositions”, but PETMDAS and PEJMDAS don’t roll of the tongue as easily.
Applying all that to the expression 6/2(2+1)
means that it’s the same as 6/2*(2+1)
, which is equal to 6/2*3 = 3*3 = 9
. I’m not sure why one wouldn’t just write the multiplication sign in this case, but coefficients in front of groups are common and many prefer not having to write the multiplication signs. The 2
is a coefficient but the term is just 2
(with no variables and therefore no multiplication of higher precedence).
But this means that 6/2a
where a := 2+1
does not equal the above expression. In this case the 2
is the coefficient of the term 2a
. It is equal to 6/(2*a) = 6/(2*(2+1)) = 6/(2*3) = 6/6 = 1
. That’s because 2 is the coefficient of a term with a variable and therefore the implied multiplication has higher precedence.
The solutions are still the same: 1, 9, ambiguous
And I personally still prefer a simpler order of operations where all types of implied multiplication have the same precedence. WolframAlpha doesn’t even use implied multiplication for terms (see here) and that is quite confusing for everyone that expects it and it means you often have to add brackets to your expressions.
In the end it’s still all about knowing what rules are used when you work with mathematical expressions. In algebra all the calculations are precise and unambiguous but the notation used by humans is completely arbitrary.