Arithmetic Expressions Outline 1

Arithmetic Expressions Outline 1

Arithmetic Expressions Outline 1. Arithmetic Expressions Outline 2. Expressions 3. Arithmetic Expressions 4. Unary & Binary Arithmetic Operations 5. Structure of Arithmetic Expressions/ Jargon: INTEGER-valued & REAL-valued Expressions 6. Precedence Order of Arithmetic Operations 7. Arithmetic Precedence Order Example: INTEGER Operands 8. Arithmetic Precedence Order Example: REAL Operands 9. Named Constants & Variables as Operands 10. Constant-Valued Expressions in Named Constant Initializations 11. Assignments Using Expressions 12. Using the Same Variable on Both Sides of an Assignment 13. The Meaning of Using the Same Variable on Both Sides of an Assignment 14. Single Mode Arithmetic 15. Division By Zero 16. Mixed Mode Arithmetic 17. A Mixed Mode Arithmetic Example 18. Exercise: Writing a Program See Programming in Fortran 90/95, 1st or 2nd ed, Chapter 10. 1 Expressions a + b - c * d / e ** f - 398 + g * 5981 / 15 ** h In programming, an expression is a combination of: • Operands, such as: – Literal constants – Named constants – Variables – Function invocations (which we’ll discuss later) • Operators, such as: – Arithmetic Operators ∗ Addition: + ∗ Subtraction: - ∗ Multiplication: * ∗ Division: / ∗ Exponentiation: ** – Relational Operators ∗ Equal: == ∗ Not Equal: /= ∗ Less Than: < ∗ Less Than or Equal To: <= ∗ Greater Than: > ∗ Greater Than or Equal To: >= – Logical Operators ∗ Negation: .NOT. ∗ Conjunction: .AND. ∗ Disjunction: .OR. • Parentheses Not surprisingly, an expression in a program can look very much like an expression in math (though not identical). This is on purpose. 2 Arithmetic Expressions An arithmetic expression (also called a numeric expression) is a combination of: • Numeric Operands, such as: – INTEGER & REAL literal constants (BAD BAD BAD) – INTEGER & REAL named constants (GOOD) – INTEGER & REAL variables – INTEGER-valued & REAL-valued function invocations • Arithmetic Operators, such as: – Identity: + – Negation: - – Addition: + – Subtraction: - – Multiplication: * – Division: / – Exponentiation: ** • Parentheses Some Arithmetic Expression Examples x +x -x x + y x - y x * y x / y x ** y x + y - (z / 22) * 7 3 Unary & Binary Arithmetic Operations Arithmetic operations come in two varieties: unary and binary. A unary operation is an operation that has only one operand. For example: -x Here, the operand is x, the operator is the minus sign, and the oper- ation is negation. A binary operation uses two operands. For example: y + z Here, the operands are y and z, the operator is the plus sign, and the operation is addition. Arithmetic operations are: Operation Kind Operator Usage Value Identity Unary + +x Value of x None x Value of x Negation Unary - -x Additive inverse of x Addition Binary + x + y Sum of x and y Subtraction Binary - x - y Difference between x and y Multiplication Binary * x * y Product of x times y (i.e., x · y) Division Binary / x / y Quotient of x over y (i.e., x ÷ y) Exponentiation Binary ** x ** y x to the power y (i.e., xy) 4 Structure of Arithmetic Expressions An arithmetic expression can be long and complicated. For example: a + b - c * d / e ** f Terms and operators can be mixed together in almost limitless vari- ety, but they must follow the rule that a unary operator has a term immediately to its right and a binary operator has terms on both its left and its right: -a + b - c * d / e ** f - 398 + g * 5981 / 15 ** h Parentheses can be placed around any unary or binary expression: (-a + b - c) * d / e ** ((f-398) + g * 5981 / 15) ** h Putting a term in parentheses may change the value of the expres- sion, because a term inside parentheses will be calculated first. For example: a + b * c is evaluated as “multiply b by c, then add a” but (a + b) * c is evaluated as “add a and b, then multiply by c” Note: as a general rule, you cannot put two operators in a row. Jargon: INTEGER-valued & REAL-valued Expressions An INTEGER-valued expression is an expression whose result, when evaluated, is an INTEGER. A REAL-valued expression is an expression whose result, when eval- uated, is a REAL. 5 Precedence Order of Arithmetic Operations In the absence of parentheses that explicitly state the order of opera- tions, the order of precedence (also called order of priority) is: • First: exponentiation, right to left, followed by • Second: multiplication and division, left to right, and finally • Third: addition, subtraction, identity and negation, left to right After taking into account the above rules, the expression as a whole is evaluated left to right. For example: • 1 - 2 - 3 ≡ -1 - 3 ≡ -4 but 1 - (2 - 3) ≡ 1 - (-1) ≡ 2 • 1 + 2 * 3 + 4 ≡ 1 + 6 + 4 ≡ 7 + 4 ≡ 11 but (1 + 2) * 3 + 4 ≡ 3 * 3 + 4 ≡ 9 + 4 ≡ 13 • 24 / 2 * 4 ≡ 12 * 4 ≡ 48 but 24 / (2 * 4) ≡ 24 / 8 ≡ 3 • 5 + 3 ** 6 / 3 ≡ 5 + 729 / 3 ≡ 5 + 243 ≡ 248 but 5 + 3 ** (6 / 3) ≡ 5 + 3 ** 2 ≡ 5 + 9 ≡ 14 but (5 + 3) ** (6 / 3) ≡ 8 ** (6 / 3) ≡ 8 ** 2 ≡ 64 • -2 ** 4 ≡ -16 but (-2) ** 4 ≡ 16 • -2 ** 3 ** 2 ≡ -512 but (-2 ** 3) ** 2 ≡ 64 Rule of Thumb: if you can’t remember the precedence order of the operators, use lots of parentheses. 6 Arithmetic Precedence Order Example: INTEGER Operands % cat intexprs.f90 PROGRAM intexprs IMPLICIT NONE PRINT *, ’1 - 2 - 3 = ’, 1 - 2 - 3 PRINT *, ’1 - (2 - 3) = ’, 1 - (2 - 3) PRINT *, "" PRINT *, ’ 1 + 2 * 3 + 4 = ’, 1 + 2 * 3 + 4 PRINT *, ’(1 + 2) * 3 + 4 = ’, (1 + 2) * 3 + 4 PRINT *, "" PRINT *, ’24 / 2 * 4 = ’, 24 / 2 * 4 PRINT *, ’24 / (2 * 4) = ’, 24 / (2 * 4) PRINT *, "" PRINT *, ’ 5 + 3 ** 6 / 3 = ’, 5 + 3 ** 6 / 3 PRINT *, ’ 5 + 3 ** (6 / 3) = ’, 5 + 3 ** (6 / 3) PRINT *, ’(5 + 3) ** (6 / 3) = ’, (5 + 3) ** (6 / 3) PRINT *, "" PRINT *, ’ -2 ** 4 = ’, -2 ** 4 PRINT *, ’(-2) ** 4 = ’, (-2) ** 4 PRINT *, "" PRINT *, ’ 2 ** 3 ** 2 = ’, 2 ** 3 ** 2 PRINT *, ’ (2 ** 3) ** 2 = ’, (2 ** 3) ** 2 END PROGRAM intexprs % f95 -o intexprs intexprs.f90 % intexprs 1 - 2 - 3 = -4 1 - (2 - 3) = 2 1 + 2 * 3 + 4 = 11 (1 + 2) * 3 + 4 = 13 24 / 2 * 4 = 48 24 / (2 * 4) = 3 5 + 3 ** 6 / 3 = 248 5 + 3 ** (6 / 3) = 14 (5 + 3) ** (6 / 3) = 64 -2 ** 4 = -16 (-2) ** 4 = 16 2 ** 3 ** 2 = 512 (2 ** 3) ** 2 = 64 Notice that a PRINT statement can output the value of an expres- sion. 7 Arithmetic Precedence Order Example: REAL Operands % cat realexprs.f90 PROGRAM realexprs IMPLICIT NONE PRINT *, ’1.0 - 2.0 - 3.0 = ’, 1.0 - 2.0 - 3.0 PRINT *, ’1.0 - (2.0 - 3.0) = ’, 1.0 - (2.0 - 3.0) PRINT *, "" PRINT *, ’ 1.0 + 2.0 * 3.0 + 4.0 = ’, & & 1.0 + 2.0 * 3.0 + 4.0 PRINT *, ’(1.0 + 2.0) * 3.0 + 4.0 = ’, & & (1.0 + 2.0) * 3.0 + 4.0 PRINT *, "" PRINT *, ’24.0 / 2.0 * 4.0 = ’, 24.0 / 2.0 * 4.0 PRINT *, ’24.0 / (2.0 * 4.0) = ’, 24.0 / (2.0 * 4.0) PRINT *, "" PRINT *, ’ 5.0 + 3.0 ** 6.0 / 3.0 = ’, & & 5.0 + 3.0 ** 6.0 / 3.0 PRINT *, ’ 5.0 + 3.0 ** (6.0 / 3.0) = ’, & & 5.0 + 3.0 ** (6.0 / 3.0) PRINT *, ’(5.0 + 3.0) ** (6.0 / 3.0) = ’, & & (5.0 + 3.0) ** (6.0 / 3.0) PRINT *, "" PRINT *, ’ -2.0 ** 4.0 = ’, -2.0 ** 4.0 PRINT *, ’(-2.0) ** 4 = ’, (-2.0) ** 4 PRINT *, "" PRINT *, ’ 2.0 ** 3.0 ** 2.0 = ’, & & 2.0 ** 3.0 ** 2.0 PRINT *, ’ (2.0 ** 3.0) ** 2.0 = ’, & & (2.0 ** 3.0) ** 2.0 END PROGRAM realexprs % f95 -o realexprs realexprs.f90 % realexprs 1.0 - 2.0 - 3.0 = -4.0000000 1.0 - (2.0 - 3.0) = 2.0000000 1.0 + 2.0 * 3.0 + 4.0 = 11.0000000 (1.0 + 2.0) * 3.0 + 4.0 = 13.0000000 24.0 / 2.0 * 4.0 = 48.0000000 24.0 / (2.0 * 4.0) = 3.0000000 5.0 + 3.0 ** 6.0 / 3.0 = 2.4800000E+02 5.0 + 3.0 ** (6.0 / 3.0) = 14.0000000 (5.0 + 3.0) ** (6.0 / 3.0) = 64.0000000 -2.0 ** 4.0 = -16.0000000 (-2.0) ** 4 = 16.0000000 2.0 ** 3.0 ** 2.0 = 5.1200000E+02 (2.0 ** 3.0) ** 2.0 = 64.0000000 8 Named Constants & Variables as Operands So far, many of the examples of expressions that we’ve looked at have used numeric literal constants as operands. But of course we already know that using numeric literal constants in the body of a program is BAD BAD BAD. So instead, we want to use named constants and variables as operands: % cat age.f90 PROGRAM age IMPLICIT NONE INTEGER,PARAMETER :: days_in_a_year = 365 INTEGER,PARAMETER :: hours_in_a_day = 24 INTEGER,PARAMETER :: minutes_in_an_hour = 60 INTEGER,PARAMETER :: seconds_in_a_minute = 60 INTEGER :: year_of_birth, current_year PRINT *, "Let me guess your age in seconds!" PRINT *, "What year were you born?" READ *, year_of_birth PRINT *, "What year is this?" READ *, current_year PRINT *, "I’d guess that your age is about ", & & (current_year - year_of_birth) * & & days_in_a_year * hours_in_a_day * & & minutes_in_an_hour * seconds_in_a_minute, & & " seconds." END PROGRAM age % f95 -o age age.f90 % age Let me guess your age in seconds! What year were you born? 1965 What year is this? 2000 I’d guess that your age is about 1103760000 seconds. Again notice that PRINT statements can output expressions. 9 Constant-Valued Expressions in Named Constant Initializations If we have an expression whose terms are all constants (either literal constants or named constants), then we can use that expression in the initialization of a named constant: % cat constexpr.f90 PROGRAM constant_expression IMPLICIT NONE REAL,PARAMETER :: & & C_to_F_factor = 9.0 / 5.0 REAL,PARAMETER :: & & C_to_F_increase = 32.0 REAL,PARAMETER :: & & C_water_boiling_temperature = 100.0 REAL,PARAMETER :: & & F_water_boiling_temperature = & & C_water_boiling_temperature * & & C_to_F_factor + & & C_to_F_increase PRINT *, "Water boils at ", & & C_water_boiling_temperature, & & " degrees C," PRINT *, " which is ", & & F_water_boiling_temperature, & & " degrees F." END PROGRAM constant_expression % f95 -o constexpr constexpr.f90 % constexpr Water boils at 1.0000000E+02 degrees C, which is 2.1200000E+02 degrees F.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    18 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us