The Trick Of The Mind - Little Languages Arithmetic
Written by Mike James
Monday, 07 March 2022
Article Index
The Trick Of The Mind - Little Languages Arithmetic
BODMAS
Arithmetic Expression and Programming

BODMAS

An arithmetic expression, at its simplest, is a set of numbers combined with any of the operators +, -, * and / corresponding to addition, subtraction, multiplication and division. Different symbols are used in different contexts, but these are the commonly used ones in programming.

So for example:

`1+8*2/4-3`

is a valid arithmetic expression, but you might be unclear what it means.

A simple left-to-right reading of the expression leads you to believe that the computation is:

```1+8 = 9
9*2 = 18
18/4 = 4.5
4.5-3 = 1.5```

This is very easy, but it is wrong – very wrong.

The rules of the arithmetic little language give each operation a “priority” and operations with a higher priority have to be done before operations with a lower priority. Notice that this is entirely arbitrary and, if we all agreed to do it the simple left-to-right way, then the result would be correct. Arithmetic expressions are a little language designed by man and not bestowed on us by nature. Even so there is no point in arguing – rejecting the longstanding conventions would mean that you were doomed to misunderstand all of the written arithmetic accumulated over hundreds of years.

So what are the rule and how do we apply them? The rules are simple and have already been stated, though not explained. We have to execute the instructions in priority order. If two instructions have the same priority then we take them left-to-right. So using this rule the execution of the expression is:

`1+8*2/4-3`

The highest priority instructions are the * and the / and as * is the first we encounter left-to-right the first step is:

`8*2 = 16`

Next we perform the division as it is a higher priority operation:

`16/4 = 4`

The remaining instructions, the + and – have the same priority and so working left-to-right:

`1+4 = 5`

and

`5 – 3 = 2`

This means that the answer is not 1.5 but 2 – and evaluating the expression incorrectly is more than enough to cause a bridge to collapse or a rocket to miss its target.

Notice that this “error” is not a deep error relating to a misunderstood theory or principle, it is an error in executing a program. The arithmetic expression 1+8*2/4-3 has been written by a human using the conventions involving the priority of operators. When you get it wrong you are simply misreading the intent of the creator of the expression. You are running the program incorrectly.

What the expression means depends on the rules assumed when it was written and getting the intended answer depends on following the same rules. You could say that not only are arithmetic expressions the first programming language that people encounter it is also likely the first bug too.

This peculiarity of arithmetic expressions is a cause of much anguish for school pupils trying to get the right answer. It is the cause of such teaching mnemonics such as BODMAS – Brackets, Orders, Division/Multiplication, Addition/Subtraction. which gives the priorities of the operations. The only puzzles in BODMAS is what Brackets and Orders stand for? Orders are a way of referring to powers like squared, cubed and so on. Brackets, far from being something you can skip over, have the highest priority of all and indicate operations that must be executed first. Thus our arithmetic expression could have been written:

`(1+((8*2)/4)-3)`

which looks confusing but always gives the right answer when you work out the brackets and evaluate inner brackets before you evaluate outer brackets. For example, if we pick

`((8*2)/4)`

as the bracket to start at, it is clear that we can’t work it out until we have evaluated the inner bracket (8*2) to give (16/4) = 4. Making full use of brackets and the rule that we evaluate the most deeply nested ones first, means that it is possible to ignore other priorities.

If arithmetic expressions were always written out in full with brackets then evaluation would be much easier. In practice we don’t do this, but we do use brackets to make the order of evaluation clearer.

For example:

1+(8*2/4)-3

is arguably clearer than:

`1+8*2/4-3`

The trick to writing clear arithmetic expressions is to use just enough brackets to clarify what is intended and no more.

If you wanted to enforce a strictly left-to-right evaluation, i.e. to write an expression that evaluated to 1.5, it can be written in full using brackets as:

`((((1+8)*2)/4)-3)`

In fact not all of the brackets are necessary in that they don’t alter the way the expression is calculated using priorities and to clarify an alternative meaning all we need is:

`(1+8)*2/4-3`

Understanding that the arithmetic expression is an example of a little language that needs some learned interpretation makes it easier to work with. Children are often led to believe that arithmetic expressions are somehow absolute and part of the way of the universe and if they get it wrong they are wrong. Knowing that it’s just a set of rules invented by other humans to make communication less ambiguous makes getting it wrong seem a lesser crime.

Last Updated ( Monday, 07 March 2022 )