(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 10290, 359]*) (*NotebookOutlinePosition[ 10927, 382]*) (* CellTagsIndexPosition[ 10883, 378]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Operator Precedence", "Section"], Cell[TextData[{ "This woksheet is designed to accompany Chapter 3 of Introduction to \ Scientific Programming: Computational Problem Solving Using ", StyleBox["Mathematica", FontSlant->"Italic"], " and C by Joseph L. Zachary. In it, we will use ", StyleBox["Mathematica", FontSlant->"Italic"], " to explore the concept of operator precedence." }], "Text"], Cell[CellGroupData[{ Cell["Multiplication/Division vs. Addition/Subtraction", "Subsection"], Cell[TextData[{ "Examine the ", StyleBox["Mathematica", FontSlant->"Italic"], " expression below. Try to predict what its value will be before you ask ", StyleBox["Mathematica", FontSlant->"Italic"], " 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." }], "Text"], Cell[BoxData[ \(\(\ 1 + 3/2\)\)], "Input"], Cell["\<\ The second possibility was correct: the division was performed \ first. We say that division takes precedence over addition.\ \>", "Text"], Cell[TextData[{ "Now consider this expression. What are two reasonable possible values? \ Which do you think ", StyleBox["Mathematica", FontSlant->"Italic"], " will find?" }], "Text"], Cell[BoxData[ \(2 - 3*4\)], "Input"], Cell["\<\ 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.\ \>", "Text"], Cell["\<\ 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?\ \ \>", "Text"], Cell[TextData[{ "Your experiments should reveal that in ", StyleBox["Mathematica", FontSlant->"Italic"], " (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." }], "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Multiplication vs. Division and Addition vs. Subtraction.", "Subsection"], Cell["\<\ 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.)\ \>", "Text"], Cell["What would be two possible values for this expression?", "Text"], Cell[BoxData[ \(2/3*4\)], "Input"], Cell["\<\ 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.\ \>", "Text"], Cell["\<\ 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.\ \>", "Text"], Cell["\<\ 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.\ \>", "Text"], Cell[BoxData[ \(2*3/4\)], "Input"], Cell[BoxData[ \(\(2/3\)/4\)], "Input"], Cell[BoxData[ \(4/3*2/5\)], "Input"], Cell[BoxData[ \(6*3/4*5\)], "Input"], Cell["\<\ 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. \ \>", "Text"], Cell["\<\ The following two expressions are mathematically identical; they \ differ only in the fact that their last two terms have been interchanged. \ Evaluate them.\ \>", "Text"], Cell[BoxData[ \(1\ - \ 1\ + \ 10.^\(-20\)\)], "Input"], Cell[BoxData[ \(\(\ 1\ + \ 10.^\(-20\)\ - \ 1\)\)], "Input"], Cell["The answers are not the same! Why?", "Text"], Cell["\<\ 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.\ \>", "Text"], Cell["\<\ 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.)\ \>", "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Exponentiation.", "Subsection"], Cell["\<\ What about the precedence of the exponentiation operator? See \ if you can figure out the rule by evaluating the following expressions:\ \>", "Text"], Cell[BoxData[ \(4*3^2\)], "Input"], Cell[BoxData[ \(1 + 3^2\)], "Input"], Cell[BoxData[ \(5/2^3\)], "Input"], Cell[BoxData[ \(3/10^3*7\)], "Input"], Cell["\<\ 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.\ \>", "Text"], Cell["Now try this expression.", "Text"], Cell[BoxData[ \(2^\(3^4\)\)], "Input"], Cell["\<\ The exponentiation operator is right-associative. Thus, the \ expression above is equivalent to\ \>", "Text"], Cell[BoxData[ \(2^\((3^4)\)\)], "Input"], Cell["and not", "Text"], Cell[BoxData[ \(\((2^3)\)^4\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Parentheses", "Subsection"], Cell[TextData[{ "You can override ", StyleBox["Mathematica", FontSlant->"Italic"], "'s precedence rules by using parentheses to group expressions in the order \ that you wish ", StyleBox["Mathematica", FontSlant->"Italic"], " to evaluate them. Here are some examples where we use parentheses to \ force a different evaluation order than ", StyleBox["Mathematica", FontSlant->"Italic"], " would otherwise use." }], "Text"], Cell[BoxData[ \(2*\((3 + 4)\)\)], "Input"], Cell[BoxData[ \(2^\((3*4)\)\)], "Input"], Cell[BoxData[ \(2/\((3/3)\)\)], "Input"], Cell[TextData[{ "If you have any doubt, use parentheses to make sure ", StyleBox["Mathematica", FontSlant->"Italic"], " executes your expressions in the order in which you intend." }], "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Summary", "Subsection"], Cell["There are five arithmetic operators: ^, *, /, +, and -.", "Text"], Cell["\<\ 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.\ \>", "Text"], Cell["\<\ In the absence of parentheses, operators at a higher level of \ precedence are performed before operators at a lower level of \ precedence.\ \>", "Text"], Cell["\<\ In the absence of parentheses, multiplication and division are \ performed from left to right, as are addition and subtraction.\ \>", "Text"], Cell["\<\ Parentheses can be used to explicitly control the order of \ evaluation of an expression.\ \>", "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Exercises", "Subsection"], Cell["\<\ 1. Evaluate the following expression. \ \>", "Text"], Cell[BoxData[ \(\(\ 2\ - \ 2^3*2\)\)], "Input"], Cell["\<\ 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.\ \>", "Text"], Cell["2. Evaluate the following expression. ", "Text"], Cell[BoxData[ \(\(\ 2/3*4^2\)\)], "Input"], Cell["\<\ 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.)\ \>", "Text"], Cell["3. Repeat exercise 2 for this expression.", "Text"], Cell[BoxData[ \(1 + 2/3*4 + 5\)], "Input"] }, Closed]] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{519, 600}, WindowMargins->{{78, Automatic}, {Automatic, 104}} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1731, 51, 38, 0, 54, "Section"], Cell[1772, 53, 374, 9, 68, "Text"], Cell[CellGroupData[{ Cell[2171, 66, 70, 0, 45, "Subsection"], Cell[2244, 68, 461, 11, 86, "Text"], Cell[2708, 81, 46, 1, 27, "Input"], Cell[2757, 84, 150, 3, 50, "Text"], Cell[2910, 89, 195, 6, 50, "Text"], Cell[3108, 97, 40, 1, 27, "Input"], Cell[3151, 100, 228, 4, 68, "Text"], Cell[3382, 106, 370, 7, 104, "Text"], Cell[3755, 115, 363, 8, 86, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[4155, 128, 79, 0, 29, "Subsection"], Cell[4237, 130, 223, 4, 68, "Text"], Cell[4463, 136, 70, 0, 32, "Text"], Cell[4536, 138, 38, 1, 27, "Input"], Cell[4577, 141, 226, 4, 68, "Text"], Cell[4806, 147, 261, 5, 68, "Text"], Cell[5070, 154, 223, 4, 68, "Text"], Cell[5296, 160, 38, 1, 27, "Input"], Cell[5337, 163, 42, 1, 27, "Input"], Cell[5382, 166, 40, 1, 27, "Input"], Cell[5425, 169, 40, 1, 27, "Input"], Cell[5468, 172, 331, 6, 86, "Text"], Cell[5802, 180, 182, 4, 50, "Text"], Cell[5987, 186, 60, 1, 27, "Input"], Cell[6050, 189, 66, 1, 27, "Input"], Cell[6119, 192, 51, 0, 32, "Text"], Cell[6173, 194, 187, 4, 50, "Text"], Cell[6363, 200, 339, 6, 86, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[6739, 211, 37, 0, 29, "Subsection"], Cell[6779, 213, 166, 4, 50, "Text"], Cell[6948, 219, 38, 1, 27, "Input"], Cell[6989, 222, 40, 1, 27, "Input"], Cell[7032, 225, 38, 1, 27, "Input"], Cell[7073, 228, 41, 1, 27, "Input"], Cell[7117, 231, 289, 5, 68, "Text"], Cell[7409, 238, 40, 0, 32, "Text"], Cell[7452, 240, 42, 1, 27, "Input"], Cell[7497, 243, 119, 3, 32, "Text"], Cell[7619, 248, 44, 1, 27, "Input"], Cell[7666, 251, 23, 0, 32, "Text"], Cell[7692, 253, 44, 1, 27, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[7773, 259, 33, 0, 29, "Subsection"], Cell[7809, 261, 448, 13, 86, "Text"], Cell[8260, 276, 46, 1, 27, "Input"], Cell[8309, 279, 44, 1, 27, "Input"], Cell[8356, 282, 44, 1, 27, "Input"], Cell[8403, 285, 202, 5, 50, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[8642, 295, 29, 0, 29, "Subsection"], Cell[8674, 297, 72, 0, 32, "Text"], Cell[8749, 299, 238, 5, 68, "Text"], Cell[8990, 306, 163, 4, 50, "Text"], Cell[9156, 312, 151, 3, 50, "Text"], Cell[9310, 317, 113, 3, 32, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[9460, 325, 31, 0, 29, "Subsection"], Cell[9494, 327, 64, 3, 50, "Text"], Cell[9561, 332, 52, 1, 27, "Input"], Cell[9616, 335, 237, 4, 68, "Text"], Cell[9856, 341, 56, 0, 32, "Text"], Cell[9915, 343, 46, 1, 27, "Input"], Cell[9964, 346, 188, 4, 50, "Text"], Cell[10155, 352, 58, 0, 32, "Text"], Cell[10216, 354, 46, 1, 27, "Input"] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)