Thursday, 14 May 2015

Operator Precedence

I see a number of people get the answer to the following equation wrong:
1 + 5 * 2
Depending who you talk to, it could either be 12 (6 * 2) or the appropriate and correct answer 11 (1 + 10).

Everybody knows the precedence of things, but to make matters clear, here's the list ordered in descending precedence:
  1. parenthesis (also known as brackets)
  2. exponents/roots
  3. multiplication/division
  4. addition/subtraction

Mnemonics

Several mnemonics are in use to remember the correct precedence. Below are three examples.

“Please Excuse My Dear Aunt Sally”
  • Parentheses
  • Exponents
  • Multiplication
  • Division
  • Addition
  • Subtraction

However, this mnemonic doesn't do justice to the fact that some operations actually have the same precedence, for example subtraction and addition.

“Hallo, Meneer van Dalen wacht op antwoord!”
  • Haakjes (Parentheses)
  • Machtsverheffen (Exponents)
  • Vermenigvuldigen (Multiplication)
  • Delen (Division)
  • Worteltrekken (Root)
  • Optellen (Addition)
  • Aftrekken (Subtraction)

This Dutch mnemonic is very very old (and very wrong nowadays). It indicates that roots are very low in priority. Also, if taken literally, it means there's a difference in precedence between, for example, addition and subtraction, similar to the English mnemonic.

“Hé, Mw. v/d Aorta!”

A different Dutch mnemonic (written above), also contains information regarding the equality of certain operations.
  1. Haakjes (Parentheses)
  2. Machtsverheffen (Exponents)/Worteltrekken (Root)
  3. Vermenigvuldigen (Multiplication)/Delen (Division)
  4. Aftrekken (Subtraction)/Optellen (Addition)
Unfortunately, it contains a number of abbreviations:
Mw.
mevrouw (misses)
v/d
van de (of)

References

Wikipedia - Order of operations
http://en.wikipedia.org/wiki/Order_of_operations
Wikipedia - Bewerkingsvolgorde
http://nl.wikipedia.org/wiki/Bewerkingsvolgorde

No comments:

Post a Comment