6/2(1+2)

This is presented as a math problem that has the internet perplexed. But Veli can lift the Veil of confusion. If we take a look at operator precedence the rules are pretty clear, at least in programming. Parenthesis () have precedence 1 while / and * both have precedence 3. In a programming language expressions are treated by evaluating the operations with lowest precedence numbers (in the expression) first. In this case it means to evaluate the sub-expression inside the parenthesis first. Now, because division and multiplication have the same precedence, look in the associativity column of the operator precedence table (from the link above). It says left-to-right. That means the following steps are taken when evaluating:

6 / 2 * (1 + 2) ; first parenthesis
6 / 2 * 3       ; evaluate 6 / 2
3 * 3
9

The answer is 9 and nothing else.

Some confusion is introduced in the step 6 / 2 * 3 because both operators have the same precedence. The left-to-right associativity means to evaluate the leftmost operator first. All popular programming languages does the left to right thing in this case and most calculators too. Only some old pocket calculators from the 80:s got this wrong.

Note. Mathematically speaking I am not sure though, the order might be undefined if the operators have the same precedence. Please comment if you have a good source on how associativity is defined mathematically for multiplication and division. What I gave here is the programming language version.

Annonser

Om albertveli

Grävande programmerare.
Det här inlägget postades i Matematik. Bokmärk permalänken.

Kommentera

Fyll i dina uppgifter nedan eller klicka på en ikon för att logga in:

WordPress.com Logo

Du kommenterar med ditt WordPress.com-konto. Logga ut / Ändra )

Twitter-bild

Du kommenterar med ditt Twitter-konto. Logga ut / Ändra )

Facebook-foto

Du kommenterar med ditt Facebook-konto. Logga ut / Ändra )

Google+ photo

Du kommenterar med ditt Google+-konto. Logga ut / Ändra )

Ansluter till %s