Book Cover

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

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

Arithmetic Expressions 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 worksheet is designed to accompany Chapter 2 of Introduction to Scientific Programming: Computational Problem Solving Using Maple and C by Joseph L. Zachary. In it, we will explore the use of basic arithmetic expressions in Maple. We will also explore some of the differences between the rational and floating-point numbers that are supplied by Maple. (24Sep96)


As you already know, you interact with Maple by entering a command and looking at the value that Maple then displays. The simplest kind of command is a number. When you enter it, Maple echoes its value, which is the number itself. There are various ways to write numbers in Maple.

There are integers, which are written without decimal points or exponents:

> 154;

There are fractions, which are written as ratios of two integers:

> 4/16;

Integers and fractions are collectively called rational numbers.

There are floating-point numbers, which are written with decimal points:

> 152.35;

Floating-point numbers can also be written using scientific notation:

> 152.35e15;

The "e15" part means "times 10 to the 15th power". Notice that the result is also written in scientific notation, although the appearance is more conventional.

A number with an exponent is treated as a floating-point number, even if it does not have a decimal point:

> 15235e13;

Maple reminds us that this is a floating-point number by putting a decimal point in the value that it displays.


At the prompts provided, enter rational numbers that are equal to:

(1) 175

(2) 17.5

(3) 175e-3

At the prompts provided, enter floating-point numbers that are equal to:

(4) 16

(5) 1/16

(6) 5 1/4

Simple Expressions

Numbers can be combined with the five arithmetic operations of addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (^). They are used in exactly the way with which you are familiar from arithmetic.

We add two rational numbers:

> 115 + 72;

We subtract two floating-point numbers:

> 191.3 - 72.3;

We multiply two rational numbers:

> 18723474387 * 2398723497;

We multiply two floating-point numbers:

> 18723474387. * 2398723497.;

Compare the results of the two multiplications. When Maple operates on rational numbers, it gives an exact result no matter how many digits are required. When Maple operates on two floating-point numbers, it gives the answer in scientific notation rounded to ten decimal places.

We divide two rational numbers:

> 8383832 / 283742;

We divide two floating-point numbers:

> 8383832. / 283742.;

Again, notice that Maple gives exact results for the rational division and a result rounded to ten digits for the floating-point division.

We exponentiate two rational numbers:

> 2 ^ 100;

We exponentiate two floating-point numbers:

> 2. ^ 100.;


At the prompts, compute the following values. Are the results rational numbers or floating-point numbers?

(7) The number of inches in a mile (there are 5280 feet in a mile and 12 inches in a foot).

(8) The number of bytes in a megabyte (it's 2 to the 20th power).

(9) The number of inches in a meter (there are .0254 meters in an inch).

(10) The difference between your height in feet and your best friend's height in feet.

(11) The combined weight of you and George Foreman (let's say he weighs 260 pounds).

Mixed Expressions

If an expression contains a mixture of rational and floating-point numbers, Maple first converts the rational number into floating-point form and then does the operation. Compare the results of the following operations to the ones in the previous section.

We multiply a rational number and a floating-point number:

> 18723474387 * 2398723497.;

We divide a floating-point number and a rational number:

> 8383832. / 283742;

We exponentiate a floating-point number and a rational number:

> 2. ^ 100;

In each case, the result is the same as it would have been if both of the numbers had been floating-point numbers.


We have been careful not to make any mistakes in our expressions to this point, but they will be very common when you experiment with Maple. Mistakes can be things like forgetting an operator symbol, forgetting a panthesis, putting in too many parentheses, and the like. Maple tries to explain what is wrong, but the explanations are not always clear. Use Maple to evaluate each of the expressions below, and then correct the syntax error that Maple reports.

(12) Average of 16 and 39.

> 0.5 * (16 + 39;

(13) Sum of 100 and 200.

> 10 0 + 200;

(14) Product of first ten even numbers.

> 2 * 4 * 6 * 8 * 10 12 * 14 * 16 * 18 * 20;

(15) 11 raised to the 11th power.

> 11 & 11;

Compound Expressions

Just as in arithmetic, more than one operation can be carried out in a single expression. The order in which the operations are carried out depends both on the way they are grouped with parentheses and on Maple's precedence rules.

Here we take the average of two numbers:

> (158 + 64) / 2;

Notice that parentheses are used, just as in mathematics, to indicate the order in which operations are carried out. In the absence of parentheses, the operations are not necessarily carried out from left to right:

> 158 + 64 / 2;

In the absence of parentheses, the division was performed first, followed by the addition. Once you have learned Maple's precedence rules, you will be able to predict the order in which arithmetic operations will be carried out. We will not discuss precedence rules in this worksheet. You should use parentheses whenever you are in doubt.

In a compound expression, a rational number is converted to floating-point form whenever it is involved in an operation with a floating-point number. Consider:

> (158 + 64) / 2.0;

The addition involves two rational numbers, so rational number addition is done. The sum is then converted to floating-point form so that it can be divided by a floating-point number. On ther other hand, consider:

> (158. + 64) / 2;

The sum involves a floating-point and a rational number, so the rational number is first converted to floating-point form. The sum is a floating-point number, so the divisor must be converted into floating-point form before the division can be carried out.


(16) Write an expression to calculate the number of centimeters in a mile. (There are 5280 feet in a mile, 12 inches in a foot, and 2.54 centimeters in an inch.)

(17) Write an expression to compute the grade point average of a class of 11 students, where 4 make an A (4.0), 3 make a B+ (3.3), 3 make a C (2.0), and 1 makes a C- (1.7).

(18) In some situations, the order in which conversions from rational to floating-point numbers are carried out in a compound expression can make a difference. Explain why the results of the following two expressions are different.

> 1.0 * (1/3 + 1/3 + 1/3);

> (1.0 * 1/3) + 1/3 + 1/3;

Function Application

Besides the five arithmetic operators, Maple provides many built-in functions that operate on numbers. Whereas operator symbols are written between the numbers that they operate on (their parameters), built-in function names are written in front of their parameters, just as is the convention in mathematics.

The "evalf" function expects one parameter, which is typically a rational number, and produces the closest floating-point equivalent:

> evalf(1/7);

The "sqrt" function produces the square root of its parameter. We can take the square root of a floating-point number:

> sqrt(18.0);

Or of a rational number:

> sqrt(18);

Notice how the square root of a floating-point number is written as a ten-digit approximation whereas the square root of a rational number is written exactly, even if it requires a radical to do so.

Just as we can write expressions with more than one arithmetic operation, we can write expressions with more than one built-in function:

> evalf(sqrt(18));

This takes the square root of a rational number and then converts it into floating-point form.

This expression amounts to the same thing:

> evalf(sqrt(9+9));

The "cos" function produces the cosine of its parameter. (The parameter should be expressed in radians.)

> cos(3.141592654 / 3);

Some functions take more than one parameter. For example, "evalf" will take an optional second argument, which should be an integer. The second parameter specifies the number of digits that should appear in the mantissa of the resulting floating-point number:

> evalf(1/7, 5);

Notice that when more than one parameter is passed to a function, they are separated with commas.


At the prompts, compute the following values.

(19) The exact sum of the square roots of 1, 2, 3, 4, and 5.

(20) A floating-point approximation to the sum above.

(21) The square root of the sum of the sine and the cosine of 1/4 Pi. (Use a value of 3.14159 for Pi.)

Help With Functions

There are so many functions built into Maple that it is virtually impossible to remember them all. Fortunately, Maple provides a powerful help facility. There are several ways to use it.

If you happen to know the name of a function and wish to know more about it, you can ask Maple to display help information for it. For example, let's see what Maple can tell us about the "sqrt" function. To ask for help, issue a command consisting of a question mark followed by the function name.

> ?sqrt

A help window will appear containing information about "sqrt". A help window will contain a brief explanation followed by some examples. The help for "sqrt" tells us that it works even for negative parameters.

Let's try that out.

> sqrt(-18);

Here, the "I" tells us that the result is a complex number.

If you know what kind of function you need but you're not sure what Maple calls it, Maple can also be of help. For eample, suppose that we'd like to find a function that will produce the prime factorization of an integer. Pull down the "Help" menu in the upper right-hand corner and choose the "Full Text Search" option. Enter "integer factorization" in the box labeled "Word(s)" and click on "Search". A window of functions having the words "integer factorization" in their descriptions will be displayed. Near the top of the list you'll find a likely function called "ifactor". If you click on its description and then click on "OK", its help window will appear. It turns out that this is just the function we need.

> ifactor(9792);

Finally, Maple will help you explore even if you have no idea what you're looking for. Pull down the "Help" menu again, but this time choose the "Contents" option. When you select one of the topics displayed, Maple will display a variety of subtopics. For example, selecting "Mathematics" displays some subtopics. Selecting "Algebra" displays more subtopics. Selecting "Rational Expressions" displays still more subtopics. Finally, selecting "denom," which is a built-in function, displays its help window.

> denom(3/15);


(22) Use Maple's help facility to find a way to compute the base 2 logarithm of a number. (The base 2 logarithm of 16 is 4 and of 19.2 is 4.263034406.)