Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.

Similar presentations


Presentation on theme: "Lecture III Start programming in Fortran Yi Lin Jan 11, 2007."— Presentation transcript:

1 Lecture III Start programming in Fortran Yi Lin Jan 11, 2007

2 A simple example: Roots finding Finding roots for quadratic aX^2+bX+c=0 Input a, b, c Output root(s): (-b + SQRT(b*b - 4*a*c))/(2*a) (-b - SQRT(b*b - 4*a*c))/(2*a)

3 Roots finding (cont.) start Initialize a=1, b=-5, c=6 w = (b*b – 4*a*c) R1 = (-b + sqrt(w))/(2*a) R2 = (-b - sqrt(w))/(2*a) End

4 Roots finding (cont.) PROGRAM ROOTSFINDING INTEGER a, b, c REAL x, y, z, w, r1, r2 a=1 b=-5 c=6 ! To calculate b*b – 4*a*c x = b*b y = a*c z = 4*y w = x-z r1 = (-b +SQRT(w))/(2*a) r2 = (-b – SQRT(w))/(2*a) WRITE(*, *) r1, r2 END PROGRAM Declare variables, Must at the head of block Statements: initialize variables with values statements: calculation statements:Output results

5 Roots finding (cont.) PROGRAM ROOTSFINDING INTEGER a, b, c REAL x, y, z, w, r1, r2 a=1 b=-5 c=6 ! To calculate b*b – 4*a*c x = b*b y = a*c z = 4*y w = x-z r1 = (-b +SQRT(w))/(2*a) r2 = (-b –SQRT(w))/(2*a) WRITE(*, *) r1, r2 END PROGRAM variables constants operators Keywords in Fortran90

6 Variables and constants Constants (e.g., 1, -5, 6, 4, 2) A variable (e.g., a, b, c, x, y, z, w) is a unique name which a FORTRAN program applies to a word of memory and uses to refer to it. Naming convention alphanumeric characters (letters, numerals and the underscore character) The first character must not be a letter. No case sensitivity Don’t use keywords in Fortran as variables’ names

7 Variable data types INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: Data type is important INTEGER::a=1,b=-5,c=6 REAL::r1, r2 IMPLICIT NONE needed to prevent errors. Comment out “IMPLICIT NONE” write(*, *) r1, r3 ! You want to print r1 and r2, but by mistakes you type r3

8 Declarations PROGRAM RootFinding IMPLICIT NONE INTEGER :: a, b, c REAL :: x,y,z,w REAL :: r1, r2... END PROGRAM RootFinding Declarations tell the compiler To allocate space in memory for a variable What “shape” the memory cell should be (i.e. what type of value is to be placed there) What name we will use to refer to that cell

9 Declare variables INTEGER a, b, c REAL x, y, z, w A=1 B=-5 C=6 INTEGER:: a=1, b=-5, c=6 REAL:: x, y, z, w same

10 Variable precision INTEGER(KIND=2)::a OR INTEGER(2)::a KINDBITsValue range 18-2**7 <= a < +2**7 (i.e., [-128,127]) 216-2**15 <= a < +2**15 (i.e., [-32768,-32767]) 4(default)32-2**31 <= a < +2**31 864-2**63 <= a < +2**63 11111111 1 byte = 8 bits

11 Example program test implicit none integer(1)::a=127 integer(2)::b=128 write(*,*) "a=", a, “b=“,b End PROGRAM Output a=127 b=-128

12 Variable precision (cont.) REAL(KIND=4) CHARACTER KIND=1 only KINDBITs 4(default)32 8(equivalent to DOUBLE PRECISION) 64 168*16

13 Arithmetic Expressions An arithmetic expression is formed using the operations: + (addition), - (subtraction) * (multiplication), / (division) ** (exponentiation) If the operands are integers, the result will be an integer value If the operands are real, the result will be a real value If the operands are of different types, the expression is called mixed mode.

14 Arithmetic expressions Variable operator variable operator …. E.g., 5 + 2 *3 E.g., b**2 – 4*a*c It has a value by itself E.g. The value of expression 5 + 2*3 is 11

15 Simple Expressions 1 + 3 --> 4 1.23 - 0.45 --> 0.78 3 * 8 --> 24 6.5/1.25 --> 5.2 8.4/4.2 --> 2.0 rather than 2, since the result must be of REAL type. -5**2 --> -25 12/4 --> 3 13/4 --> 3 rather than 3.25. Since the operands are of INTEGER type, so is the result. The computer will truncate the mathematical result to make it an integer. 3/5 --> 0 rather than 0.6.

16 Evaluating Complex Expressions Evaluate operators in order of precedence. First evaluate operators of higher precedence 3 * 4 – 5  7 3 + 4 * 5  23 For operators of the same precedence, use associativity. Exponentiation is right associative, all others are left associative Expressions within parentheses are evaluated first

17 Arithmetic operators: precedence 2+3*4 = ? = 5 * 4 = 20 Or = 2+12 = 14 (2+3)*4 = 20 operatorsPrecedence ()1 **2 *, /3 +, -4

18 Arithmetic operators: associativity operatorsassociativity ()Left to right **Right to left *, /Left to right +, -Left to right associativity resolves the order of operations when two operators of the same precedence compete for three operands: 2**3**4 = 2**(3**4) = 2**81, 72/12/ 3 = (72/12)/3 = 6/3 = 2 30/5*3 = (30/5)*3 = 18 if *,/ associativity is from right to left 30/5*3 = 30/(5*3) = 2

19 Examples 2 * 4 * 5 / 3 ** 2 --> 2 * 4 * 5 / [3 ** 2] --> 2 * 4 * 5 / 9 --> [2 * 4] * 5 / 9 --> 8 * 5 / 9 --> [8 * 5] / 9 --> 40 / 9 --> 4 The result is 4 rather than 4.444444 since the operands are all integers.

20 Examples 100 + (1 + 250 / 100) ** 3 --> 100 + (1 + [250 / 100]) ** 3 --> 100 + (1 + 2) ** 3 --> 100 + ([1 + 2]) ** 3 --> 100 + 3 ** 3 --> 100 + [3 ** 3] --> 100 + 27 --> 127 Parentheses are evaluated first

21 Examples 1.0 + 2.0 * 3.0 / ( 6.0*6.0 + 5.0*44.0) ** 0.25 --> 1.0 + [2.0 * 3.0] / (6.0*6.0 + 5.0*44.0) ** 0.25 --> 1.0 + 6.0 / (6.0*6.0 + 5.0*55.0) ** 0.25 --> 1.0 + 6.0 / ([6.0*6.0] + 5.0*44.0) ** 0.25 --> 1.0 + 6.0 / (36.0 + 5.0*44.0) ** 0.25 --> 1.0 + 6.0 / (36.0 + [5.0*44.0]) ** 0.25 --> 1.0 + 6.0 / (36.0 + 220.0) ** 0.25 --> 1.0 + 6.0 / ([36.0 + 220.0]) ** 0.25 --> 1.0 + 6.0 / 256.0 ** 0.25 --> 1.0 + 6.0 / [256.0 ** 0.25] --> 1.0 + 6.0 / 4.0 --> 1.0 + [6.0 / 4.0] --> 1.0 + 1.5 --> 2.5

22 Mixed Mode Expressions If one operand of an arithmetic operator is INTEGER and the other is REAL the INTEGER value is converted to REAL the operation is performed the result is REAL 1 + 2.5  3.5 1/2.0  0.5 2.0/8  0.25 -3**2.0  -9.0 4.0**(1/2)  1.0 (since 1/2  0)

23 Example of Mixed Mode 25.0 ** 1 / 2 * 3.5 ** (1 / 3)  25.0 ** 1 / 2 * 3.5 ** (1 / 3)  25.0 ** 1 / 2 * 3.5 ** ([1 / 3])  [25.0 ** 1] / 2 * 3.5 ** 0  25.0 / 2 * [3.5 ** 0]  25.0 / 2 * 1.0  [25.0 / 2] * 1.0  12.5 * 1.0  12.5

24 Statements Assignment statement: Variable = expression e.g., b = -5 (never the other way around, show an example) General statement: INTEGER a, b, c write (*,*) ‘Hello world!’

25 Assignment Statement The assignment statement has syntax: variable = expression Semantics 1. Evaluate the expression 2. If the type of result is the same as the type of the variable store the result in the variable 3. Otherwise convert the value to the type of the variable and then store it – If the value is REAL and the variable is INTEGER remove the decimal part (truncate) – If the value is INTEGER and the variable is REAL convert the value to a decimal 4. The original value of the variable is destroyed

26 Assignment statements examples REAL::X=3.5 X=2 * 2.4 ! X=4.8 INTEGER X=3 X=2 * 2.4 ! X=4 X=2 * 2.6 ! X=5

27 Roots finding revisited PROGRAM ROOTSFINDING INTEGER::a=1, b=-5, c=6 REAL x, r1, r2 ! To calculate b*b – 4*a*c x = b**2- 4*a*c r1 = (-b +SQRT(x))/(2*a) r2 = (-b – SQRT(x))/(2*a) WRITE(*, *) r1, r2 END PROGRAM Declare variables, Must at the head of block statements: calculation statements:Output results

28 Example in a previous final PROGRAM Q1 IMPLICIT NONE INTEGER :: I, J, K REAL :: A, B, C A = 3.2 + 4 B = 22/7 C = 22.0/7.0 I = 3.7 + MOD(78,5) J = 45/3*3+1-3*4 K = C WRITE(*,*) I,J,K,A,B,C END PROGRAM Q1 a) 63437.2003.0003.143 b) 7.200 3.000 3.143 6 34 3 c) 6.700 34.000 3.143 7.200 3.000 3.143 d) 6 34 3 7.200 3.143 3.143

29 Example in midterm05 What does the following program output? PROGRAM midterm REAL A,B,C INTEGER I,J,K A = 3.5 I = A J = 5.25 K = I*2 B = A*I C = J/3 WRITE (*,*) A,B,C,I,J,K END PROGRAM midterm A. 3.5 10.5 1. 3 5 7 B. 3.5 12.25 1. 35 6 C. 3.5 10.5 1. 35 6 D. 3.5 10.5 1.75 35 6 E. None of the above


Download ppt "Lecture III Start programming in Fortran Yi Lin Jan 11, 2007."

Similar presentations


Ads by Google