{VERSION 2 3 "IBM INTEL NT" "2.3" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 }{CSTYLE "" -1 256 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 } {PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Heading 1" 0 3 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 0 0 0 0 0 0 0 }1 0 0 0 6 6 0 0 0 0 0 0 -1 0 }{PSTYLE "Bullet Item" 0 15 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 3 3 0 0 0 0 0 0 15 2 }{PSTYLE "Title" 0 18 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 1 0 0 0 0 0 0 }3 0 0 -1 12 12 0 0 0 0 0 0 19 0 }} {SECT 0 {PARA 18 "" 0 "" {TEXT -1 19 "Operator Precedence" }}{PARA 0 " " 0 "" {TEXT -1 52 "This woksheet is designed to accompany Chapter 3 o f " }{TEXT 256 87 "Introduction to Scientific Programming: Computation al Problem Solving Using Maple and C" }{TEXT -1 109 " by Joseph L. Zac hary. In it, we will use Maple to explore the concept of operator pre cedence. (AW, Jan 97)" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 48 "Multiplication/Division vs. Addition/Subt raction" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 316 "Examine the Maple expr ession below. Try to predict what its value will be before you ask Ma ple to evaluate it. There seem to be two possibilities. If the addit ion is performed first, the result will be 4/2, which is 2. If the di vision is performed first the result will be 5/2. Which is it? Try i t out and see." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "1+3/2;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 1 "T" }{TEXT -1 124 "he second possibility wa s correct: the division was performed first. We say that division t akes precedence over addition." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 108 "Now consider this expression. What are \+ two reasonable possible values? Which do you think Maple will find?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "2- 3*4;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 200 "Here, the multiplication was performed prior to the subtractio n. (Had the subtraction been performed first, the answer would have b een -4.) We say that multiplication takes precedence over addition." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 34 "Now try the following experiments:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 15 "" 0 "" {TEXT -1 146 "In the first expression above, replace the + \+ operator with a - operator and reevaluate. Which operator takes prece dence, subtraction or division?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 15 "" 0 "" {TEXT -1 150 "In the second expression above, replace the - operator with a + operator and reevaluate. Which operator take s precedence, addition or multiplication?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 272 "Your experiments should reveal th at in Maple (as in most other programming languages) multiplication an d 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." }}}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {SECT 1 {PARA 3 "" 0 "" {TEXT -1 56 "Multiplication vs. Division and A ddition vs. Subtraction" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 194 " What \+ happens when an expression involves more than operator at the same lev el of precedence? (For example, more than one multiplication and divi sion or more than one addition and subtraction.)" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 54 "What would be two possibl e values for this expression?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "2/3*4;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 198 "The division was performed first (to obtain 2/3), and the result was then multiplied by 4 (to ob tain 8/3). Had the multiplication been performed before the division, the result would have been 1/6." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 232 "This does not mean that multiplication t akes precedence over division, however. In the absence of parenthese s, multiplication and division are performed left to right. We say th at multiplication and division are left associative." }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 195 "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 w here the flaw in your reasoning occurred." }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "2*3/4;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "2/3/4 ;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "4/3*2/5;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "6*3/4*5;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 299 "Addition and subtractio n are also left associative. In the absence of parentheses, addition \+ and multiplication is performed from left to right. In rational arith metic (at least in the absence of overlow) this does not make any diff erence, but in floating-point arithmetic it can become important. " }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 154 "The foll owing two expressions are mathematically identical; they differ only i n the fact that their last two terms have been interchanged. Evaluate them." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "1 - 1 + 1e-12;" }}{PARA 0 "" 0 "" {TEXT -1 1 " " }}}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "1 + 1 e-12 - 1;\n" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 " " {TEXT -1 35 "The answers are not the same! Why?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 157 "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." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 307 "In the s econd expression, the addition is performed first. With only ten digi ts 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.)" }}}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 14 "Exponentiation" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 135 "What about the precedence of the exponentiation operator ? See if you can figure out the rule by evaluating the following exp ressions:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "4*3^2;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "1+3^2;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "5/2^3;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "3/10^3*7 ;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 259 "Notice that in each of these examples, the exponentiation was per formed first. This is indeed the way that it works: exponentiation ha s higher precedence than multiplication and division, which in turn ha ve higher precedence than addition and multiplication." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 24 "Now try this express ion." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "2^3^4;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 " " {TEXT -1 169 "The exponentiation operator is non-associative. If yo u wish to do more than one exponentiation in an expression, you must u se parentheses to tell Maple whether you want" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "(2^3)^4;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 2 "or" }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "2^(3 ^4);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 11 "Parentheses" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 278 "Speaking of parentheses, you can override Mapl e's precedence rules by using parentheses to group expressions in the \+ order that you wish Maple to evaluate them. Here are some examples wh ere we use parentheses to force a different evaluation order than Mapl e would otherwise use." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 8 "2*(3+4);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "2^(3*4);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "2/(3/3);" }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 117 "I f you have any doubt, use parentheses to make usre Maple executes your expressions in the order in which you intend." }{MPLTEXT 1 0 0 "" }}} }{PARA 0 "" 0 "" {TEXT -1 0 "" }}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 7 "S ummary" }}{PARA 15 "" 0 "" {TEXT -1 56 "There are five arithmetic oper ators: ^, *, /, +, and -." }}{PARA 15 "" 0 "" {TEXT -1 208 "Exponenti ation (^) is at the highest level of precedence, multiplication (*) an d division (/) are at a lower level of precedence, and addition (+) an d subtraction (-) are at an even lower level of precedence." }}{PARA 15 "" 0 "" {TEXT -1 135 "In the absence of parentheses, operators at a higher level of precedence are performed before operators at a lower \+ level of precedence." }}{PARA 15 "" 0 "" {TEXT -1 125 "In the absence \+ of parentheses, multiplication and division are performed from left to right, as are addition and subtraction." }}{PARA 15 "" 0 "" {TEXT -1 87 "Parentheses can be used to explicitly control the order of evaluat ion of an expression." }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 9 "Exercises" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 40 "1. Evaluate the following expression. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "2 - 2^3*2;" }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 209 "Add one \+ set of parentheses so that the expression evalutes to -12. Now move y our parentheses so that the expression evaluates to -62. Move your pa rentheses one last time to make the expression evaluate to 0." }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 42 "2. Evaluate the following expression. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "2/3*4^2;" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 159 "Add two sets of parenthe ses that leave the value of the expression unchanged. (Your parenthes es may enclose neither the entire expression nor a single number." }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 42 "3. Repeat exercise 2 for this expression." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "1+2/3*4+5;" }}}}}{MARK "0 0 " 0 }{VIEWOPTS 1 1 0 1 1 1803 }