This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Handout Three October 13, 2011 1 Arithmetic Expressions with Integers & Reals Mostly you will find that typing in arithmetic expressions is intuitive and does not really cause any problems. However, there are situations that can occur where uncertainty arises and so you need to be aware of certain rules that exist to remove any confusion. 1.1 Expression Evaluation In section 2.2 of handout two the intrinsic numeric operators and their order of precedence were listed in a table, for convenience, here is the table again; note that the / & * operators have the same precedence. Then below that the dyadic minus (- ) operator has the same level of precedence as the plus ( + ) operator .  ** : Exponentiation operator, dyadic  / : Division operator, dyadic  * : Multiplicative operator, dyadic - : minus operator monadic for negation  + : additive operator, dyadic - : minus operator, dyadic An example was given to demonstrate how the order is important when writing down numeric expressions in Fortran. The left to right rule was introduced for cases where there was ambiguity as to which order to evaluate sub expressions involving operators with the same level of precedence. There is however one exception to this left to right rule. The exception involves a special case of the exponentiation operator. The exponentiation operator has a precedence right to left. For example consider the expression 2 1 a**b**c The numbers above the operators in the expression indicate the order in which that sub-expression will be evaluated. First b**c would be calculated and the result used as the exponent in the operation with a . Consider the following two lengthy expressions, again, above each of the operators is a number identifying the order in which that operator is executed during the calculation of the overall expression. 6 3 2 1 4 7 5 6 3 1 2 4 7 5 a-b/c**d**e*f+g*h a-b/(c**d)**e*f+g*h For the expression on the left, the first calculation is d**e and the result used as the exponent in the exponentiation operation on c then the result divided into b etc. and so on. Work through the above expression and make sure you understand it before moving on! Also, although it is important that you know about correct expression evaluation if you are in doubt you can use parentheses () to remove any uncertainty! See how for the expression on the right the inclusion of parentheses around the c**d alter the default order of the right to left rule for exponentiation. Remember sub-expressions inside parentheses have the highest precedence so will be evaluated first! 1 NOTE : No two numeric operators can co-exist side by side. For example 4*-3 would fail during compilation. You need to include parentheses around the monadic expression : 4*(-3) . You could however have written -3*4 ....
View Full Document
- Fall '10