 ### Introduction to Scientific Programming Computational Problem Solving Using: Maple and C Mathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

# Operator Precedence Notebook

Click below to download a Mathematica notebook. You can look at the appended non-interactive HTML version of the notebook to learn what the notebook covers.

### Operator Precedence

This woksheet is designed to accompany Chapter 3 of Introduction to Scientific Programming: Computational Problem Solving Using Mathematica and C by Joseph L. Zachary. In it, we will use Mathematica to explore the concept of operator precedence.

#### Multiplication/Division vs. Addition/Subtraction

Examine the Mathematica expression below. Try to predict what its value will be before you ask Mathematica to evaluate it. There seem to be two possibilities. If the addition is performed first, the result will be 4/2, which is 2. If the division is performed first the result will be 5/2. Which is it? Try it out and see.

`      1 + 3/2`

The second possibility was correct: the division was performed first. We say that division takes precedence over addition.

Now consider this expression. What are two reasonable possible values? Which do you think Mathematica will find?

`     2 - 3 * 4`

Here, the multiplication was performed prior to the subtraction. (Had the subtraction been performed first, the answer would have been -4.) We say that multiplication takes precedence over addition.

Now try the following experiments:
(1) In the first expression above, replace the + operator with a - operator and reevaluate. Which operator takes precedence, subtraction or division?
(2) In the second expression above, replace the - operator with a + operator and reevaluate. Which operator takes precedence, addition or multiplication?

Your experiments should reveal that in Mathematica (as in most other programming languages) multiplication and division take precedence over addition and subtraction. We say that multiplication and division are at a higher level of precedence than are addition and subtraction.

#### Multiplication vs. Division and Addition vs. Subtraction.

What happens when an expression involves more than operator at the same level of precedence? (For example, more than one multiplication and division or more than one addition and subtraction.)

What would be two possible values for this expression?

`     2/3 * 4`

The division was performed first (to obtain 2/3), and the result was then multiplied by 4 (to obtain 8/3). Had the multiplication been performed before the division, the result would have been 1/6.

This does not mean that multiplication takes precedence over division, however. In the absence of parentheses, multiplication and division are performed left to right. We say that multiplication and division are left associative.

Try to predict the outcome of each of the following calculations before you try it. If your prediction turns out to be incorrect, be sure to figure out where the flaw in your reasoning occurred.

`     2 * 3/4`

`     2/3/4`

`     4/3 * 2/5`

`     6 * 3/4 * 5`

Addition and subtraction are also left associative. In the absence of parentheses, addition and multiplication is performed from left to right. In rational arithmetic (at least in the absence of overlow) this does not make any difference, but in floating-point arithmetic it can become important.

The following two expressions are mathematically identical; they differ only in the fact that their last two terms have been interchanged. Evaluate them.

`     1 - 1 + 10.^-20`

`      1 + 10.^-20 - 1`

The answers are not the same! Why?

In the first expression, the subtraction is performed first and the result is zero. When zero is added to the number 10^-20, we end up with the result 10^-20.

In the second expression, the addition is performed first. With only 16 digits of mantissa, however, the number 10^-20 is the lost in the roundoff and the result is 1. The subtraction thus yields a result of zero. (This example shows that floating-point arithmetic need not satisfy the associative rule.)

#### Exponentiation.

What about the precedence of the exponentiation operator? See if you can figure out the rule by evaluating the following expressions:

`     4 * 3^2`

`     1 + 3^2`

`     5/2^3`

`     3/10^3 * 7`

Notice that in each of these examples, the exponentiation was performed first. This is indeed the way that it works: exponentiation has higher precedence than multiplication and division, which in turn have higher precedence than addition and multiplication.

Now try this expression.

`     2^3^4`

The exponentiation operator is right-associative. Thus, the expression above is equivalent to

`     2^(3^4)`

and not

`     (2^3)^4`

#### Parentheses

You can override Mathematica's precedence rules by using parentheses to group expressions in the order that you wish Mathematica to evaluate them. Here are some examples where we use parentheses to force a different evaluation order than Mathematica would otherwise use.

`     2 * (3 + 4)`

`     2^(3 * 4)`

`     2/(3/3)`

If you have any doubt, use parentheses to make sure Mathematica executes your expressions in the order in which you intend.

#### Summary

There are five arithmetic operators: ^, *, /, +, and -.

Exponentiation (^) is at the highest level of precedence, multiplication (*) and division (/) are at a lower level of precedence, and addition (+) and subtraction (-) are at an even lower level of precedence.

In the absence of parentheses, operators at a higher level of precedence are performed before operators at a lower level of precedence.

In the absence of parentheses, multiplication and division are performed from left to right, as are addition and subtraction.

Parentheses can be used to explicitly control the order of evaluation of an expression.

#### Exercises

1. Evaluate the following expression.

`      2 - 2^3 * 2`

Add one set of parentheses so that the expression evalutes to -12. Now move your parentheses so that the expression evaluates to -62. Move your parentheses one last time to make the expression evaluate to 0.

2. Evaluate the following expression.

`      2/3 * 4^2`

Add two sets of parentheses that leave the value of the expression unchanged. (Your parentheses may enclose neither the entire expression nor a single number.)

3. Repeat exercise 2 for this expression.

`     1 + 2/3 * 4 + 5`