## 6/2(1+2)

At LDTA this year, we had a talk that focused on… whitespace.  Then we spent a few hours fascinated by pretty printing.  Why these “trivial” things? Well, because there are interesting things here to talk about, and believe it or not, hard problems to solve. (How DO you easily and declaratively specify how whitespace should behave in a language where the rules are not completely uniform?)

Why do I mention this? Well, the reason is this: the answer to the question in the title (evaluates to 1 or 9?) is not one you were definitively taught in grade school.  I know, I know… it’s “Pluto’s not a planet” and “raptors are tiny and have feathers” all over again, isn’t it?  Sometimes the obvious things aren’t so obvious.

Most people seem to just seize on claiming, quite correctly, that 6/2*(1+2) = 9. That’s correct, but that’s correct because you’ve implicitly assumed juxtaposition has the same precedence as multiplication. (note that here we’ve introduced a *) Is that a safe guess?

I don’t think that’s a completely safe guess, and I’d back that up by just pointing to the sheer number of people who thought the answer is 1. Why would they think that?

Probably because we expect terms to be written is a convenient normal form.  Juxtapose all factors of the numerator, write divide, then juxtapose all the factors of the denominator.  After all, doesn’t $\frac{abc}{def}$ seem more naturally written as $abc/def$ than $abc/d/e/f$?  (Though of course, there’s always $abc/(def)$.)  The rule to allow this is quite easy and unambiguous: it just makes juxtaposition higher precedence than multiplication.

But, conventions have to be, well, convention.  So the best answers seem to be, in order:

1. Write the term to conform to the normal form we expect: 6(1+2)/2, and call whoever wrote this a jerk!
2. 9. Juxtaposition=Multiplication, left associativity!
3. Let’s agree to make juxtaposition higher precedence because it reads better. Then this term would unambiguously be 1.

So in conclusion, calling this ambiguous isn’t entirely wrong, but there’s probably only one way you should interpret it. But ambiguous mathematical notation is hardly rare: $\log^2(x)$ is either $(\log(x))^2$ or $\log(\log(x))$, depending on who you ask.

Now, the really important questions: what would the mnemonic for PEJMDAS be? And does anyone actually use the mnemonic to remember order of operations? And when will juxtaposition release its birth certificate to prove it’s a real operator?

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

### 2 Responses to 6/2(1+2)

1. dgatwood says:

If you give juxtaposition higher precedence, that precedence must extend only over operations that would other wise be the same level of precedence or lower. For example, you would not want the juxtaposition rule to cause people to interpret 2x^2 as (2x)^2.

That said, I think I agree with #1 best. As they always taught us in programming classes, when in doubt, parenthesize. :-)

2. Brusno says:

Perhaps you see the ambiguity better if you start with 6/6 going backwards:
1 = 6/6 = 6/(2+4) = 6/2(1+2) = 6/2*(1+2) = 3 * (3) = 9