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

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Types and Arithmetic Operators
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing.
1 ICS 101 – LAB 2 Arithmetic Operations I Putu Danu Raharja kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Chapter 2 Data Types, Declarations, and Displays
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Objectives You should be able to describe: Data Types
CS0004: Introduction to Programming Variables – Numbers.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Conditional Control Flow Constructs. Sequential Control Flow Execution order follows the textual order straight line flow Many simple problems can not.
Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
SOLVING QUADRATIC EQUATIONS BY COMPLETING THE SQUARE BECAUSE GRAPHING IS SOMETIMES INACCURATE, ALGEBRA CAN BE USED TO FIND EXACT SOLUTIONS. ONE OF THOSE.
Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
Introduction to Exponents Definition: Let b represent a real number and n a positive integer. Then … b is called the base and n is called the exponent.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Copyright © Curt Hill The C++ IF Statement The most important decision statement Part 1.
C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Chapter 7: Expressions and Assignment Statements
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 4: Expressions and Variables
Building Java Programs
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Revision Lecture
Primitive Data, Variables, Loops (Maybe)
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 3 Operators and Expressions
Chapter 7 Expressions and Assignment Statements.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 4: Expressions and Variables
Building Java Programs
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
DATA TYPES AND OPERATIONS
Building Java Programs
Building Java Programs
Presentation transcript:

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

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 km a year A year is 365 days, 5 hours, 48 minutes and seconds The average distance between the earth and sun is 150,000,000 km

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 / ( * 24.0 * 60.0) distance = * 10.0 ** 6 time = distance / light_minute write (*,*) "Light from the sun takes ", time, & "minutes to reach earth." end program light_travel

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

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

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

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

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

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

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

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

Fall, 2006Introduction to FORTRAN12 Precedence Rules  First evaluate operators of higher precedence 3 * 4 – 5  * 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  ?

Fall, 2006Introduction to FORTRAN13 Precedence Rules  First evaluate operators of higher precedence 3 * 4 – 5  * 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)

Fall, 2006Introduction to FORTRAN14 Precedence Rules  First evaluate operators of higher precedence 3 * 4 – 5  * 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

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

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

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

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

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

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?

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?

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?

Fall, 2006Introduction to FORTRAN23 Real Numbers (examples) E-3 (0.001) 150.0E6 But not: 1, E5 12.0E1.5

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)

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 to DOUBLE numbers: Mantissa represented by 53 bits gives about 15 decimal digits of precision Exponent represented by 11 bits gives range from to

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

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

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/( * 24.0 * 60.0) distance = * 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

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

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

Fall, 2006Introduction to FORTRAN31 Some Simple Examples   * 8  /1.25  /4.2  2.0 (not 2)

Fall, 2006Introduction to FORTRAN32 Some Simple Examples   * 8  /1.25  /4.2  2.0 (not 2) -5**2  -25 (not precedence)

Fall, 2006Introduction to FORTRAN33 Some Simple Examples   * 8  /1.25  /4.2  2.0 (not 2) -5**2  -25 (not 25) 3/5  0 (not 0.6) 3./5.  0.6

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 since the operands are all integers.

Fall, 2006Introduction to FORTRAN35 Still More Examples * 3.0 / ( 6.0* *44.0) ** > [2.0 * 3.0] / (6.0* *44.0) ** > / (6.0* *55.0) ** > / ([6.0*6.0] + 5.0*44.0) ** > / ( *44.0) ** > / ( [5.0*44.0]) ** > / ( ) ** > / ([ ]) ** > / ** > / [256.0 ** 0.25] --> / > [6.0 / 4.0] --> > 2.5

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)

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)

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

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

Fall, 2006Introduction to FORTRAN40 Mixed Mode Examples  3.5 1/2.0  /8  **2.0  **(1/2)  1.0 (since 1/2  0)

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

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/( * 24.0 * 60.0) distance = * 10**6 time = distance / light_minute write (*,*) "Light from the sun takes ", time, & "minutes to reach earth." end program light_travel

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

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?

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?

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

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

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)

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

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

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