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

Arithmetic Expressions

This notebook is designed to accompany Chapter 2 of "Introduction to Scientific Programming: Computational Problem Solving Using Mathematica and C" by Joseph L. Zachary. In it, we will explore the use of basic arithmetic expressions in Mathematica. We will also explore some of the differences between the rational and floating-point numbers that are supplied by Mathematica. (25Jul1997)


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

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


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


Integers and fractions are collectively called rational numbers.

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


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


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


In the space provided, enter rational numbers that are equal to:

(1) 175.


(2) 17.5


(3) 175.*^-3


In the space 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 Mathematica operates on rational numbers, it gives an exact result no matter how many digits are required. When Mathematica operates on two floating-point numbers, it gives the answer in scientific notation rounded to six decimal places.

We divide two rational numbers:

     8383832 / 283742

We divide two floating-point numbers:

     8383832. / 283742.

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

We exponentiate two rational numbers:

     2 ^ 100

We exponentiate two floating-point numbers:

     2. ^ 100.

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, Mathematica 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 Mathematica. Mistakes can be things like forgetting an operator symbol, forgetting a panthesis, putting in too many parentheses, and the like. Mathematica tries to explain what is wrong, but the explanations are not
always clear. Use Mathematica to evaluate each of the expressions below, and then correct the syntax error.

(12) Average of 16 and 39.

     0.5 * (16 + 39

(13) Sum of 10 and 200.

     10 5 + 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 Mathematica'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 Mathematica'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 notebook. 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).


Function Applications

Besides the five arithmetic operators, Mathematica 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 "N" function expects one parameter, which is typically a rational number, and produces the closest floating-point equivalent:


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


Or of a rational number:


Notice how the square root of a floating-point number is written as a six-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:


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

This expression amounts to the same thing:

     N[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, "N" 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:

     N[1/7, 25]

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


Compute the following values.

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


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


(20) 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

If you happen to know the name of a function and wish to know more about it, you can ask Mathematica to display help information for it. For example, let's see what Mathematica 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

You can get more detailed information by using the help browser. Choose the "Help..." option from the "Help" menu, enter "Sqrt" to the left of the "Go To:" button, and then click the button.

Mathematica will help you explore even if you have no idea what you're looking for. When you select one of the topics displayed in the help browser, Mathematica will display a variety of subtopics. For example, selecting "Algebraic Computation" in the first column displays some subtopics. Selecting "Formula Manipulation" in the second column displays more subtopics. Finally, selecting "Denominator" in the third column displays help information for that built-in function.


(21) Use Mathematica'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.)