Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006.

Similar presentations


Presentation on theme: "Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006."— Presentation transcript:

1 Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006

2 Introduction to FORTRAN2 The Speed of Light How long does it take light to travel from the sun to earth? Light travels 9.46 x 10 12 km a year A year is 365 days, 5 hours, 48 minutes and 45.9747 seconds The average distance between the earth and sun is 150,000,000 km

3 Fall, 2006Introduction to FORTRAN3 Elapsed Time program light_travel implicit none real :: light_minute, distance, time real :: light_year = 9.46 * 10.0 ** 12 light_minute = light_year / (365.25 * 24.0 * 60.0) distance = 150.0 * 10.0 ** 6 time = distance / light_minute write (*,*) "Light from the sun takes ", time, & "minutes to reach earth." end program light_travel

4 Fall, 2006Introduction to FORTRAN4 Arithmetic Expressions An arithmetic expression is formed using the operations: + (addition), - (subtraction) * (multiplication), / (division) ** (exponentiation)

5 Fall, 2006Introduction to FORTRAN5 Watch out for ambiguity Let’s look at two expressions from our program light_minute = light_year / (365.25 * 24.0 * 60.0) distance = 150.0 * 10.0 ** 6

6 Fall, 2006Introduction to FORTRAN6 Watch out for ambiguity Let’s look at two expressions from our program light_minute = light_year / (365.25 * 24.0 * 60.0) distance = 150.0 * 10.0 ** 6 What value is assigned to distance?

7 Fall, 2006Introduction to FORTRAN7 Watch out for ambiguity Let’s look at two expressions from our program light_minute = light_year / (365.25 * 24.0 * 60.0) distance = 150.0 * 10.0 ** 6 What value is assigned to distance? (150.0 * 10.0) ** 6 ? 150.0 * (10.0 ** 6) ?

8 Fall, 2006Introduction to FORTRAN8 Watch out for ambiguity Let’s look at two expressions from our program light_minute = light_year / (365.25 * 24.0 * 60.0) distance = 150.0 * 10.0 ** 6 What value is assigned to distance? (150.0 * 10.0) ** 6 ? 150.0 * (10.0 ** 6) ? What if the first expression didn’t have parentheses? light_minute = light_year / 365.25 * 24.0 * 60.0

9 Fall, 2006Introduction to FORTRAN9 Precedence Rules Every language has rules to determine what order to perform operations For example, in FORTRAN ** comes before * In an expression, all of the **’s are evaluated before the *’s

10 Fall, 2006Introduction to FORTRAN10 Precedence Rules  First evaluate operators of higher precedence 3 * 4 – 5  ? 3 + 4 * 5  ?

11 Fall, 2006Introduction to FORTRAN11 Precedence Rules  First evaluate operators of higher precedence 3 * 4 – 5  7 3 + 4 * 5  23

12 Fall, 2006Introduction to FORTRAN12 Precedence Rules  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 5 – 4 – 2  ? 2 ** 3 ** 2  ?

13 Fall, 2006Introduction to FORTRAN13 Precedence Rules  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 5 – 4 – 2  -1 (not 3) 2 ** 3 ** 2  512 (not 64)

14 Fall, 2006Introduction to FORTRAN14 Precedence Rules  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 5 – 4 – 2  -1 (not 3) 2 ** 3 ** 2  512 (not 64)  Expressions within parentheses are evaluated first

15 Fall, 2006Introduction to FORTRAN15 Precedence of Operators in FORTRAN Operators in order of precedence and their associativity: Arithmetic ** right to left *, / left to right +, - left to right Relational, >=, ==, /= no associativity Logical.NOT. right to left.AND. left to right.OR. left to right.EQV.,.NEQV. left to right

16 Fall, 2006Introduction to FORTRAN16 Another Example Last lecture we looked at the problem of finding the roots of a quadratic equation We focused on the discriminant Here is a program that computes the roots

17 Fall, 2006Introduction to FORTRAN17 ! -------------------------------------------------- ! Solve Ax^2 + Bx + C = 0 ! -------------------------------------------------- PROGRAM QuadraticEquation IMPLICIT NONE REAL :: a, b, c REAL :: d REAL :: root1, root2 ! read in the coefficients a, b and c WRITE(*,*) 'A, B, C Please : ' READ(*,*) a, b, c ! compute the square root of discriminant d d = SQRT(b*b - 4.0*a*c) ! solve the equation root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root ! display the results WRITE(*,*) 'Roots are ', root1, ' and ', root2 END PROGRAM QuadraticEquation

18 Fall, 2006Introduction to FORTRAN18 Data Types In the examples we have declared the variables to be of type REAL

19 Fall, 2006Introduction to FORTRAN19 Data Types In the examples we have declared the variables to be of type REAL That is, each memory cell can hold a real number

20 Fall, 2006Introduction to FORTRAN20 Data Types In the examples we have declared the variables to be of type REAL That is, each memory cell can hold a real number What is a real number?

21 Fall, 2006Introduction to FORTRAN21 Data Types In the examples we have declared the variables to be of type REAL That is, each memory cell can hold a real number What is a real number? In Mathematics?

22 Fall, 2006Introduction to FORTRAN22 Data Types In the examples we have declared the variables to be of type REAL That is, each memory cell can hold a real number What is a real number? In Mathematics? In FORTRAN?

23 Fall, 2006Introduction to FORTRAN23 Real Numbers (examples) 3.14159 10.0 10. -123.45 +1.0E-3 (0.001) 150.0E6 But not: 1,000.000 123E5 12.0E1.5

24 Fall, 2006Introduction to FORTRAN24 Real Numbers (representation) A real value is stored in two parts 1. A mantissa determines the precision 2. An exponent determines the range Real numbers are typically stored as either 32 bits (4 bytes): type REAL 64 bits (8 bytes): type DOUBLE (This varies on some computers)

25 Fall, 2006Introduction to FORTRAN25 Accuracy of Real Numbers REAL numbers: Mantissa represented by 24 bits gives about 7 decimal digits of precision Exponent represented by 8 bits gives range from 10 -38 to 10 38 DOUBLE numbers: Mantissa represented by 53 bits gives about 15 decimal digits of precision Exponent represented by 11 bits gives range from 10 -308 to 10 308

26 Fall, 2006Introduction to FORTRAN26 2 + 2 = ??? Be careful not to expect exact results with real numbers **** example from laptop

27 Fall, 2006Introduction to FORTRAN27 Back to The Speed of Light How long does it take light to travel from the sun to earth? The result we got was a decimal number of minutes We’d rather have the number of minutes and seconds

28 Fall, 2006Introduction to FORTRAN28 Minutes and Seconds program light_travel implicit none real :: light_minute, distance, time real :: light_year = 9.46 * 10.0**12 integer :: minutes, seconds light_minute = light_year/(365.25 * 24.0 * 60.0) distance = 150.0 * 10**6 time = distance / light_minute minutes = time seconds = (time - minutes) * 60 write (*,*) "Light from the sun takes ", minutes, & " minutes and ", seconds, " seconds to reach earth." end program light_travel

29 Fall, 2006Introduction to FORTRAN29 Integer Numbers Integers are whole numbers represented using 32 (or sometimes 16 or even 64 bits) For 32 bit numbers whole numbers with up to nine digits can be represented 0 -987 +17 123456789

30 Fall, 2006Introduction to FORTRAN30 Integer Arithmetic The result of performing an operation on two integers is an integer This may result in some unexpected results since the decimal part is truncated 12/4  3 13/4  3 1/2  0 2/3  0

31 Fall, 2006Introduction to FORTRAN31 Some Simple Examples 1 + 3  4 1.23 - 0.45  0.78 3 * 8  24 6.5/1.25  5.2 8.4/4.2  2.0 (not 2)

32 Fall, 2006Introduction to FORTRAN32 Some Simple Examples 1 + 3  4 1.23 - 0.45  0.78 3 * 8  24 6.5/1.25  5.2 8.4/4.2  2.0 (not 2) -5**2  -25 (not 25 -- precedence)

33 Fall, 2006Introduction to FORTRAN33 Some Simple Examples 1 + 3  4 1.23 - 0.45  0.78 3 * 8  24 6.5/1.25  5.2 8.4/4.2  2.0 (not 2) -5**2  -25 (not 25) 3/5  0 (not 0.6) 3./5.  0.6

34 Fall, 2006Introduction to FORTRAN34 Another Example 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.

35 Fall, 2006Introduction to FORTRAN35 Still More 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

36 Fall, 2006Introduction to FORTRAN36 Mixed Mode Assignment In the speed of light example, we assigned an real value to an integer variable minutes = time The value being assigned is converted to an integer by truncating (not rounding)

37 Fall, 2006Introduction to FORTRAN37 Mixed Mode Assignment In the speed of light example, we assigned an real value to an integer variable minutes = time The value being assigned is converted to an integer by truncating (not rounding) When assigning an integer to a real variable, the integer is first converted to a real (the internal representation changes)

38 Fall, 2006Introduction to FORTRAN38 Mixed Mode Expressions In the speed of light example, we subtracted the integer value, minutes, from the real value, time seconds = (time - minutes) * 60

39 Fall, 2006Introduction to FORTRAN39 Mixed Mode Expressions In the speed of light example, we subtracted the integer value, minutes, from the real value, time seconds = (time - minutes) * 60 If an operation involves an integer and a real, the integer is first converted to a real and then the operation is done. The result is real. The example has two arithmetic operations, an assignment and forces two type conversions

40 Fall, 2006Introduction to FORTRAN40 Mixed Mode Examples 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)

41 Fall, 2006Introduction to FORTRAN41 Another Example 25.0 ** 1 / 2 * 3.5 ** (1 / 3)  [25.0 ** 1] / 2 * 3.5 ** (1 / 3)  25.0 / 2 * 3.5 ** (1 / 3)  25.0 / 2 * 3.5 ** ([1 / 3])  25.0 / 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

42 Fall, 2006Introduction to FORTRAN42 Something’s Not Right Here program light_travel implicit none real :: light_minute, distance, time real :: light_year = 9.46 * 10**12 light_minute = light_year/(365.25 * 24.0 * 60.0) distance = 150.0 * 10**6 time = distance / light_minute write (*,*) "Light from the sun takes ", time, & "minutes to reach earth." end program light_travel

43 Fall, 2006Introduction to FORTRAN43 What Happened? Look at this assignment: light_year = 9.46 * 10**12 Precedent rules tell us that 10**12 is evaluated first Type rules tell us that the result is an integer Integers can only have about 9 digits, not 12

44 Fall, 2006Introduction to FORTRAN44 How do we fix it? Let’s change light_year = 9.46 * 10**12 to light_year = 9.46 * 10.0**12 That works, but why?

45 Fall, 2006Introduction to FORTRAN45 Back to roots of a quadratic Let’s have another look at our program for finding the roots of a quadratic equation We used the classic formula that involved finding the square root of the discriminant What if the disciminant is negative?

46 Fall, 2006Introduction to FORTRAN46 ! -------------------------------------------------- ! Solve Ax^2 + Bx + C = 0 ! -------------------------------------------------- PROGRAM QuadraticEquation IMPLICIT NONE REAL :: a, b, c REAL :: d REAL :: root1, root2 ! read in the coefficients a, b and c WRITE(*,*) 'A, B, C Please : ' READ(*,*) a, b, c ! compute the square root of discriminant d d = SQRT(b*b - 4.0*a*c) ! solve the equation root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root ! display the results WRITE(*,*) 'Roots are ', root1, ' and ', root2 END PROGRAM QuadraticEquation

47 Fall, 2006Introduction to FORTRAN47 A Run Time Error If the disciminant is negative, attempting to take the square root would cause an error during execution This is called a run time error and the program would abort

48 Fall, 2006Introduction to FORTRAN48 Selection What can we do? Every programming language must provide a selection mechanism that allows us to control whether or not a statement should be executed This will depend on whether or not some condition is satisfied (such as the discriminant being positive)

49 Fall, 2006Introduction to FORTRAN49 ! ------------------------------------------------------------ ! Solve Ax^2 + Bx + C = 0 ! ------------------------------------------------------------ PROGRAM QuadraticEquation IMPLICIT NONE ! **** Same old declarations and set up **** ! compute the square root of discriminant d d = b*b - 4.0*a*c IF (d >= 0.0) THEN ! is it solvable? d = SQRT(d) root1 = (-b + d)/(2.0*a) root2 = (-b - d)/(2.0*a) WRITE(*,*) "Roots are ", root1, " and ", root2 ELSE ! complex roots WRITE(*,*) "There is no real root!" WRITE(*,*) "Discriminant = ", d END IF END PROGRAM QuadraticEquation

50 Fall, 2006Introduction to FORTRAN50 FORTRAN Selection Used to select between two alternative sequences of statements. The keywords delineate the statement blocks. Syntax: IF (logical-expression) THEN first statement block, s 1 ELSE second statement block, s 2 END IF

51 Fall, 2006Introduction to FORTRAN51 Semantics of IF…THEN…ELSE…END IF  Evaluate the logical expression. It can have value.TRUE. or value.FALSE.  If the value is.TRUE., evaluate s 1, the first block of statements  If the value is.FALSE., evaluate s 2, the second block of statements  After finishing whichever of s 1 or s 2 that was chosen, execute the next statement following the END IF


Download ppt "Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006."

Similar presentations


Ads by Google