VIEWS: 59 PAGES: 22 CATEGORY: Computers & Internet POSTED ON: 2/1/2010 Public Domain
Fortran -90 Dipan Ghosh 1 Fortran Character Set • A to Z • a to z • 0 to 9 • Underscore _ • + - * / ** • ( ) ,(comma) .(period) = ‘ (single quote) “ (double quote) : ! % ; < > ? $ 2 Structure of a Fortran Program • Declaration statements • Executable statements • Terminal section 3 Some special characters • ! is used for comments, any thing that comes after ! Is ignored by the compiler • If a statement is too long, i.e. it cannot be accommodated in a single line it can be continued to the next line by ending the current line with & (and optionally starting the next line with another &) • ; is used to separate two statements written in a single line. 4 Declaration Staements • The first statement is a program name which MUST start with an alphabet and may use alphabet, digits and underscore as subsequent characters. E.g. Program First0608_07 • The last statement is the program end e.g. end program First0608_07 • Fortran is NOT case sensitive (unlike C) 5 Data Types • Integer (negative and positive numbers including zero) • Real (number with a decimal point) • Character • Logical (tales two values .TRUE. .FALSE.) • Derived Types (we will not discuss these) 6 Real constants and variables • A decimal point is required • 156. 0 or 156. • Exponent notation 1.25E05 0r 12.5E04 etc. The mantissa may be positive or negative. • Exponent has to be an integer positive or negative 7 Character constants and variables • Character constants or variables to be enclosed within single quote or a double quote. In case the quotes themselves are required use two consecutive quotes, e.g. to use the string Hobson’s choice, we have to either use “Hobson’s choice” or ‘Hobson’’s choice’ 8 Logical constants and variables • Takes only two values true or false • Must be enclosed within two dots .TRUE. Or .FALSE. • Numerical value of 0 is associated with .FALSE. And 1 with .TRUE. 9 IMPLICIT variable type • If not explicitly declared otherwise, any variable name which starts with I,J,K,L, M or N are integers and all others real. A variable name can contain digits or underscore (_) but must start with an alphabet. • IMPLICIT NONE requires every variable to be explicitly declared. 10 Declaration of variables • INTEGER :: x,y,z, sum, var1, factor_2 • REAL :: • LOGICAL :: • CHARACTER(len=) – Len tells us the character length, if not specified it takes value 1 – One can ignore the word len= and simply put the length within parantheses. 11 Declaration of constants • These are values which are not changed during a program’s life • Real, parameter :: pi = 3.141592 • Integer, parameter :: months_in_a_year=12 • Character, parameter :: error=“unknown” – In this the character length is automatically fixed by the value assigned. 12 Assignment • A variable name can be assigned a value by an equal to sign. Examples : – x=5.0 assign the variable x to value 5. – x=x+1 means assign to the variable x, the current value of x plus 1. This statement implies that the right hand side is known. – The right hand side evaluates to a value. For instance, it could be – x= x*5.0-(2*x+5)**2/3 13 Arithmetic operators and precedence • Binary operators + - * / and exponentiation **. Two operators cannot appear next to each other • Incorrect x*-y , correct x*(-y) • Incorrect x**-2 , correct x**(-2) • Incorrect x(y+z), correct x*(y+z) • Parantheses should be used for clarity 14 Arithmetic operators and precedence • Parantheses are evaluated forst starting with innermost pair. • Exponentiation comes next evaluated from RIGHT to LEFT • Division and multiplication comes next evaluated from left to right. • Additions and subtraction done next from left to right. 15 Intrinsic Functions • Standard mathematical functions are included in Fortran • Sqrt(real), sin(real in radians), cos, tan,asin, acos, atan, exp(real), log(real,+ve), log10(+ve, real) all return real values. • Int(real) returns integer truncated, NINT(real) returns integer rounded, real(integer) converts integer to real. MAX(a,b) , MIN(a,b) and MOD(a,b) takes int and real argument and returns same type as the argument given. 16 An example • Evaluate 3.*(6.0/(2.+3.)+1.)*2.**3**2 Evaluate innermost bracket 3.*(6.0/5.+1.)*2.**3**2 3.*(1.2+1.)*2.**3**2 3.*2.2*2.**3**2 3.*2.2*2.**9 3.*2.2*512. = 3.*1126.4=3379.2 17 Mixed Mode Arithmetic • An operation performed with an integer and a real number is called mixed mode. The compiler converts the integer to a real before performing the operation • 2.3 + 5/4 + 2*3.0 is mixed mode but 5/4 is integer mode evaluates to 1 • 2*3.0 = 2.*3.0 = 6.0 (mixed mode) • 2.3+1 = 2.3+1.0 = 3.3 (mixed mode) • 3.3+6.0=6.3 (real mode) 18 Mixed mode assignment • Integer :: I ; Real :: x,y,z x=2.5 I =x evaluates to I=2 y= x**z is valid only if x is non-negative because it is evaluated as y= exp(zlog x) 19 Logical Operator • Evaluates to .true. Or .false. • The operators are – ==, /=, >, <, <=, >= • 3==6/2 evaluates to .true. • 3>4 evaluates to .false. • Combination of relation – .AND., .OR. , .EQV., .NEQV. , .NOT. 20 Initialization of variables • Variables may be initialized along with declaration • Integer :: iter=0 • Real :: velocity=1.5, acceleration = 0.3 • Character(len=7) :: ‘Fortran’ 21 Intrinsic Functions • Standard mathematical functions are included in Fortran • Sqrt(real), sin(real in radians), cos, tan,asin, acos, atan, exp(real), log(real,+ve), log10(+ve, real) all return real values. • Int(real) returns integer truncated, NINT(real) returns integer rounded, real(integer) converts integer to real. MAX(a,b) , MIN(a,b) and MOD(a,b) takes int and real argument and returns same type as the argument given. 22