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 inequalities) cleanly and unambiguously. For example, it is understood that an expression like $4x-7y$ is different from either $4(x-7y)$ or $(4x-7)y$ .

For standard arithmetic, operator precedence is as follows:

"1. Parenthesization,

2. Factorial,

4. Multiplication and division,

Thus, in the expression $1+3(2^{2}!-1)$ , the subtraction of 1 from $2^{2}!$ 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 "Please excuse my dear Aunt Sally" (parentheses, exponents and roots, multiplication, division, addition, subtraction).

## Implied parentheses

When expressions are not limited to a single line, parentheses are often implied. For example, in ${\frac {a}{b+c}}$ , one understands that the addition of $b$ to $c$ is to be done before dividing $a$ by that quantity.

Of course if one has to write that expression on a single line, it is necessary to use parentheses: $a\div (b+c)$ .

The extenders of root symbols also generate implied parentheses that must be made explicit for single-line expressions. In the quadratic formula, for example, $x={\frac {-b\pm {\sqrt {b^{2}-4ac}}}{2a}},$ it is quite clear that the subtraction of $4ac$ from $b^{2}$ is to be done before trying to divide anything by $2a$ . On a single line without root symbol extenders, on the other hand, parentheses are very much needed: $x=(-b\pm {\sqrt {}}(b^{2}-4ac))/2a$ .

## Same-level precedence

In some cases, an expression contains operations that are all on the same level. For example, $4\div 7\times 3$ . Multiplication and division are at the same level of precedence. Of necessity, one of the two operations has to be listed first, and this should not be understood to imply that multiplication is at a slightly higher level of precedence.

Generally, such expressions should be evaluated left to right. Thus, in the example, the first thing to do is divide 4 by 7, and then one multiplies the intermediate result by 3.

If one intends for the multiplication of 7 by 3 is to be done first, one should add parentheses to override left-to-right evaluation: $4\div (7\times 3)$ . Since parentheses are at the highest level of precedence, this is no longer a same-level precedence expression.

When expressions are not limited to a single line, operator precedence is generally clearer, e.g., ${\frac {4}{7}}\times 3\neq {\frac {4}{7\times 3}}.$ In some older texts, parentheses are sometimes implied for what appear to be same-level precedence expressions. Contributors to the OEIS are encouraged to make explicit parentheses that were implied by forgotten customs, so as to conform with modern practice.

## Expressions with iteration operators

In more complicated expressions, humans generally understand by common sense where parentheses are implied. Thus, in an expression such as $a_{n}=\prod _{i=0}^{n-1}a_{i}+1$ , with $a_{0}=2$ , a human understands that the one-step increment of $a_{i}$ 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 $a_{n}=1+\prod _{i=0}^{n-1}a_{i}$ . Either way, the human also understands that it is a good idea to evaluate $n-1$ 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 $\prod _{n=1}^{\infty }\left(1-{\frac {(-1)^{n}}{\phi ^{2n}}}\right)$ even though it should be quite clear that $\left(\prod _{n=1}^{\infty }1\right)=1$ and is not what Finch intended here (and in any case, $n$ would be "out of scope" by the time we got around to evaluating ${\frac {(-1)^{n}}{\phi ^{2n}}}$ ).

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

## Operator precedence in computer programming

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 $\left(a_{i}+1\right)$ rather than $a_{i}+1$ 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."

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).