Saturday, September 24, 2011

Operator precedence

At the back of the box of a popular brand of hot chocolate mix are several activities for children and one of them is to determine the average number of mini marshmallows in a serving of hot cocoa by solving the equation 3+2x4÷2-3x7-4+47.  My son happily evaluates this equation from left to right and came up with the answer 92 which is the same as the answer given at the bottom of the box.  My wife took a look at what he was doing and said: "Wait a minute, that is not the way we were taught in school. There is a precedence of operators, multiplication and division before addition and subtraction."  Furthermore, when the part of the equation contains both multiplication and division, the evaluation proceeds from left to right.  Similarly for addition and subtraction.  Using this rule, the correct answer is 3+(2x4÷2)-(3x7)-4+47 = 29 (which coincidentally is 92 reversed), a much smaller number of marshmallows.  It is not clear how the rule of operator precedence evolved, but the development of computer programming languages such as FORTRAN necessitates this disambiguation of the syntax of mathematical equations (see the webpost here).