This site is supported by donations to The OEIS Foundation.

# Operator precedence

The rules of **operator precedence** help mathematicians write expressions (such as equations and integrals) cleanly and unambiguously. For example, it is understood that an expression like is different from either or .^{[1]}

For standard arithmetic, operator precedence is as follows:

"1. Parenthesization,

2. Factorial,

3. Exponentiation,

4. Multiplication and division,

5. Addition and subtraction."^{[2]}

Thus, in the expression , the subtraction of 1 from would be evaluated before the addition because the subtraction is part of an expression within parenthesis.

However, some instructors may choose to leave out factorials. A mnemonic sometimes used by students is "**P**lease **e**xcuse **m**y **d**ear **A**unt **S**ally" (**p**arentheses, **e**xponents and roots, **m**ultiplication, **d**ivision, **a**ddition, **s**ubtraction).^{[3]}

In more complicated expressions, humans generally understand by common sense where parentheses are implied. Thus, in an expression such as , with , a human understands that the one-step increment of is to be performed prior to the iterated multiplications (see A004168). If the author had meant the Sylvester sequence (A000058), he should have had the good sense to write . Either way, the human also understands that it is a good idea to evaluate prior to carrying out any multiplications.

There are some authors who nevertheless add parentheses in such expressions. Steven Finch, for example, gives the formula for the Fibonacci factorial constant (A062073) as ^{[4]} even though it should be quite clear that and is not what Finch intended here (and in any case, would be "out of scope" by the time we got around to evaluating ).

That last example also shows that the negation operator has lower precedence than exponentiation, making it necessary to parenthesize whenever it is used in an iterated expression, since is essentially useless. When mathematical typesetting is not available, then it generally becomes necessary to add even more parentheses. For example, would have to be given as `((-1)^n)/(phi^(2n))`

to properly convey the meaning.

Computers, being generally less adept at coping with ambiguity than humans, need to have all operators defined for precedence and associativity, including comparison operators like =, <, >. In the first example expression above, even an advanced CAS like Mathematica needs to be given the expression rather than to give the desired result.

Ford and Teory write that "Misunderstanding of the operator precedence rules often can lead to expressions that evaluate data incorrectly." The authors then asked readers to consider a C++ program with the following line of code:

milesPerGallon = endMileage - startMileage / gallonsUsed;

"Programmers often make liberal use of parentheses to prevent this kind of error in the first place."^{[5]}

Failure to observe operator precedence can snare even those well aware of the concept. To double-check the identity for squares (A001254) of Lucas numbers (A000032), one could try the following Mathematica command: Table[LucasL[2n] - LucasL[n]^2 - 2 (-1)^n, {n, 100}]. The sequence of 4s with alternating signs should not inspire confidence. Adding parentheses like so: Table[LucasL[2n] - (LucasL[n]^2 - 2 (-1)^n), {n, 100}] gives the desired sequence of all zeroes (A000004).

- ↑ The possibility that there may be values of either variable that cause the expressions to evaluate the same should not distract from the inadvisability of treating them as equivalent. For the first two examples, reduces both expressions to .
- ↑ Pegg, Ed Jr. "Precedence." From MathWorld--A Wolfram Web Resource, created by Eric W. Weisstein. http://mathworld.wolfram.com/Precedence.html
- ↑ Rhonda Huettenmueller,
*Algebra Demystified: A Self-Teaching Guide*McGraw-Hill New York (2003): 164 - ↑ here being of course the golden ratio, A001622. S. R. Finch, Mathematical Constants, Cambridge, 2003, Section 1.2.5. "Fibonacci Factorials" p. 10
- ↑ Chapter 2, "Common Syntax and Semantic Errors" from Ann R. Ford & Toby J. Teorey
*Practical Debugging in C++*Upper Saddle River, New Jersey: Prentice Hall Inc. 2002, p. 16