
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 floatingpoint 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:
154
There are fractions, which are written as ratios of two integers:
4/16
Integers and fractions are collectively called rational numbers.
There are floatingpoint numbers, which are written with decimal points:
152.35
Floatingpoint numbers can also be written using scientific notation:
152.35*^15
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 floatingpoint numbers that are equal to:
(4) 16
(5) 1/16
(6) 5 1/4
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 floatingpoint numbers:
191.3  72.3
We multiply two rational numbers:
18723474387 * 2398723497
We multiply two floatingpoint 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 floatingpoint numbers, it gives the answer in scientific notation rounded to six decimal places.
We divide two rational numbers:
8383832 / 283742
We divide two floatingpoint numbers:
8383832. / 283742.
Again, notice that Mathematica gives exact results for the rational division and a result rounded to six digits for the floatingpoint division.
We exponentiate two rational numbers:
2 ^ 100
We exponentiate two floatingpoint numbers:
2. ^ 100.
Compute the following values. Are the results rational numbers or floatingpoint 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).
If an expression contains a mixture of rational and floatingpoint numbers, Mathematica first converts the rational number into floatingpoint 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 floatingpoint number:
18723474387 * 2398723497.
We divide a floatingpoint number and a rational number:
8383832. / 283742
We exponentiate a floatingpoint 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 floatingpoint 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
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 floatingpoint form whenever it is involved in an operation with a floatingpoint number. Consider:
(158 + 64) / 2.0
The addition involves two rational numbers, so rational number addition is done. The sum is then converted to floatingpoint form so that it can be divided by a floatingpoint number. On ther other hand, consider:
(158. + 64) / 2
The sum involves a floatingpoint and a rational number, so the rational number is first converted to floatingpoint form. The sum is a floatingpoint number, so the divisor must be converted into floatingpoint 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).
Besides the five arithmetic operators, Mathematica provides many builtin functions that operate on numbers. Whereas operator symbols are written between the numbers that they operate on (their parameters), builtin 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 floatingpoint equivalent:
N[1/7]
The "Sqrt" function produces the square root of its parameter. We can take the square root of a floatingpoint number:
Sqrt[18.0]
Or of a rational number:
Sqrt[18]
Notice how the square root of a floatingpoint number is written as a sixdigit 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 builtin function:
N[Sqrt[18]]
This takes the square root of a rational number and then converts it into floatingpoint 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 floatingpoint 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 floatingpoint 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.)
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 builtin function.
Denominator[3/15]
(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.)