 ### Introduction to Scientific ProgrammingComputational Problem Solving Using:Maple and CMathematica and C

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

# Operator Precedence Worksheet

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

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

Examine the Maple expression below. Try to predict what its value will be before you ask Maple 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 Maple 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:

• In the first expression above, replace the + operator with a - operator and reevaluate. Which operator takes precedence, subtraction or division?

• 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 Maple (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 + 1e-12;

> 1 + 1e-12 - 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 1e-12, we end up with the result 1e-12.

In the second expression, the addition is performed first. With only ten digits of mantissa, however, the number 1e-12 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 non-associative. If you wish to do more than one exponentiation in an expression, you must use parentheses to tell Maple whether you want

> (2^3)^4;

or

> 2^(3^4);

# Parentheses

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

> 2*(3+4);

> 2^(3*4);

> 2/(3/3);

If you have any doubt, use parentheses to make usre Maple 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;