How do I count thee? Let me count the ways?

"You get a free drink if you order a Burger or a Salad and a Dessert."

      A restaurant advertises, "You get a free drink if you order a Burger or a Salad and a Dessert." Suppose you only orde...

Showing posts with label order of operations. Show all posts
Showing posts with label order of operations. Show all posts

Monday, November 16, 2026

"You get a free drink if you order a Burger or a Salad and a Dessert."

      A restaurant advertises, "You get a free drink if you order a Burger or a Salad and a Dessert." Suppose you only order a Burger. Should the restaurant give you a free drink?

      Employee #1 says, "Burger alone gets the free drink!" Employee #2 says, "No, you need dessert too!"

      How different is this question than the calculation of 1 + 2 * 3 ? Many people would do the calculation left to right and get 9. Mathematics has established conventions for the order of operation rules (remember the acronym PEMDAS, or BODMAS?), such as parentheses P come first (evaluate expressions inside parentheses first), and multiplication M precedes (is performed before) addition A. Under those rules, math people would do the calculation as 1 + 2 * 3 = 1 + (2 * 3) and calculate it as 7.

      The burger question essentially asks, which operation takes precedence, OR or AND? Should we interpret A or B and C as (A or B) and C, or else as A or (B and C)? Unlike mathematical notation, ordinary English does not necessarily provide an unambigious answer.

      Although the burger question sounds silly, what if we asked an A or B and C in a web search? "Please provide a list of businesses offering services involving cats or dogs and providing instruction"? Or what if your homeowners insurance policy contained a policy exclusion that is of the form A or B and C?

      For convenience, let's call the burger A, the salad B, and the dessert C. In the original example, you only order a burger, so let's agree that A = True, B = False, and C = False.

      To a non-math person, A or B and C is probably ambiguous. Such a person might evaluate it left to right. Then A or B and C becomes (A or B) and C = (True or False) and False = True and False = False, so no free drink. Or, maybe the common customer expectation is that they deserve the free drink, which essentially says AND has precedence over OR, so A or (B and C) = True or (False and False) = True or False = True, so yes a free drink.

      A court might rule in favor of the common customer expectation, partucularly if the restaurant's wording is considered misleading. If the restaurant wrote a confusing promotion, the customer may get the benefit of the doubt. In insurance law there is a legal principle that when the policy was written solely by the insurer, ambiguities are interpreted strictly in favor of the policyholder.

      What would Microsoft Excel say? You can enter TRUE in cell A1, and FALSE in cells B1 and C1. Excel has AND and OR functions. But you can not normally test precedence because Excel forces you to nest one function inside the other, so you force the priority. What you can do is use the fact that TRUE equals 1 and FALSE equals 0, and that AND is similar to multiplication while OR is similar to addition. So if cell D1 contains the formula = (A1 + B1 * C1) > 0 the result is TRUE, so yes a free drink.

      Programming languages have their own precedence rules, but both R and Python give precedence to AND over OR.

      Bottom line: The restaurant's wording is ambiguous when viewed as a logical expression. A math or programming person would give AND precedence over OR, interpreting the offer as A or (B and C). That means the burger alone gets a free drink. Interestingly, this may also be what the ordinary customer would understand from the wording, although they might arrive at that interpretation therough ordinary English rather than mathematical rules.

      The lesson: Don't leave the interpretation of AND and OR to chance. Use parentheses, or rewrite the sentence to make the intention unmistakenable.

      The following code examples demonstrate actual programming precedence.



# calculate A or B and C:
# In R:
A <- TRUE
B <- FALSE
C <- FALSE
print(A | B & C)   # result is TRUE

# In Python:
A = True
B = False
C = False
print(A or B and C)   # result is True

Cartoon from Microsoft Copilot
End

Friday, April 7, 2023

A more interesting pictorial numerical puzzle

I am getting tired of these little pictorial numerical puzzles with the four equations, like one where three chickens equals 60, one chicken plus two plates of two eggs per plate equals 26, and so on, until the final equation is to evaluate some mathematical expression involving chickens, eggs, and bananas.

The solution generally requires that you remember the PEMDAS (in the US, or BODMAS elsewhere) rules for order of operations especially that multiplication takes precedence over addition, and also that you carefully count the number of eggs and number of bananas. I get 36.

OK, let me try to create a more interesting pictorial puzzle.

Mathematicians agree on the PEMDAS rules, although there are many situations that PEMDAS doesn't handle. Perhaps the most common is the unary minus operator as in -32. It is unary because unlike subtraction that has two operands, the unary operator only has one. I think mathematicians would like to see the unary operator as changing the sign of the argument, so that -32 equals -9, although some software, most notably Excel, merrily calculate this as +9.

I don't believe there is a single authority for all the order of operations cases. For example, Excel, Google Search, and Wolfram Alpha do not always agree. I bet there are some pretty smart people in those companies.

Nowadays I am doing my fun calculations in the R computer language, so for the remainder of this post I will require R as the authority.

So here is my attempt at a more interesting problem, but remember, you have to use the order of operation precedence rules of R: (Let me add the link to the first item: https://www.facebook.com/watch/?v=10158293605695705 )

Do you want to try it before I reveal the R code?



The R code is:

apple <- 1
banana <- 2
kiwi <- 3
lemon <- 4
peach <- banana + lemon
pear <- banana^banana^kiwi     # 2^(2^3) = 256
pineapple <- (pear - banana) %% kiwi^2 * lemon     # (254 %% 9) * 4 = 2 * 4 = 8
strawberry <- pineapple / peach * peach     # 8; no obelus in R
kiwi <- c(lemon, pineapple, strawberry)
watermelon <- kiwi[kiwi == lemon | kiwi == pineapple & kiwi == strawberry]
# watermelon <- (lemon V pineapple ∧ strawberry)     # 4 V (8 & 8) = 4, 8

The ordering rules of R include:

  • Modular arithmetic is at the same level as multiplication:     a mod b * c is     (a mod b) *c
  • The obelus does not appear in R but is just a division symbol:     a ÷ b * c = (a / b) * c
  • Repeated exponentiation goes right to left: a ^ b ^ c = a^(b^c); lots of disagreement outside R on this one
  • Logical AND preceds logical OR

Friday, February 15, 2013

Let's stop making excuses for Aunt Sally


Let's stop making excuses for dear Aunt Sally. You know who I mean: Please Excuse My Dear Aunt Sally. Many students can still remember the mnemonic PEMDAS for the rules of order of operations, but PEMDAS omits or confuses some situations. Most prominently, even my current college algebra textbook does not make it clear that in the M and D of PEMDAS multiplication and division are of equal priority, so a division is performed before a multiplication if the division appears to the left of the multiplication.

I like to invite my students to use all their calculators on expressions like 300 - 200/50*10 to see which calculators obey the rules of operations and which do not. I am always surprised when students first turn to the calculator on their cell phone to answer this.

PEMDAS doesn't address fraction bars, nor that with multiple grouping symbols you start with the innermost grouping symbols first. And I suppose it's too much to expect that PEMDAS address that in -32 the negation precedes the exponentiation; by the way, Excel disagrees with this.

Speaking of exponentiation, what do you do with x y z ? Exponentiation is not associative. Johnny Lott in A Problem Solving Approach to Mathematics says exponentiation is done in order from right to left (page 276), although Excel disagrees with this one too.

So there's more to the rules of order of operation than PEMDAS. But if you are getting a little tired of Aunt Sally, why not switch to Please Email My Dad A Shark, by the authors of xkcd.com

Anyone have any good ideas on teaching order of operations?

GFU2TT2BG3ZC