Title: An Example Program
1An Example Program
PROGRAM convert_time ! Comments start with an
exclamation point IMPLICIT NONE INTEGER hours,
mins, secs, temp WRITE(,) Input the hours,
seconds, and minutes READ(,) hours, mins,
secs temp 60 (hours 60 mins)
secs WRITE(,) Time in seconds , temp END
PROGRAM convert_time
2Data Types
INTEGER for exact whole numbers e.g 1, 21,
-152, 34265,
REAL for approximate fractional numbers e.g 1.2,
21.3, -1.52, 34265.45, ?,
COMPLEX for complex fractional numbers e.g (1.5,
-4.67)
LOGICAL for truth (boolean) values These may
either be .TRUE. or .FALSE.
CHARACTER for strings of characters e,g Type
a number, Result ,
3Integers
Integer represent exact whole numbers
Integers are restricted to a finite range
Typically ?2147483647 (-231 to 231-1)
(4 bytes)
Sometimes ?9.23 ? 1017 (-263 to 263-1)
(8 bytes)
4Integers
Integers are typically used for
Loop counts and loop limits
Index in an array or a position in a list
Index of a character in a string
Error codes
For purely integer values e.g. counting seconds,
counting money
5Real Numbers
Real numbers are held as floating point values
They have a finite range and a finite precision
They are used for continuously varying values
Real constants must have a decimal point They
can look anything like -12.0, 120.34,
0.000123, 0.000123, 0.0, 0.000, .000
They can have an optional E, or D and an
exponent 1.0E4 ----gt 10000. in single precision
(4Bytes) 1.0D4 ----gt 10000. in double precision
(8Bytes)
or 123.0E-3, 0.0123D3,
6Complex Numbers
Complex numbers correspond to a pair of two REAL
numbers
Example complex z1, z2
!consists of a 2 pairs of complex numbers z1
(0., 1.) z2 (0., 1.) write(,) z1 z2
Writes on screen (-1.00000, 0.000000)
7Declaration of Numeric Variables
A numeric variable can hold values of different
types
integer i ! standard
4-byte integer real density
! standard 4-byte floating point
Sometimes you see (Lahey ED does not support
this)
integer4 i ! standard
4-byte integer integer8 long
! 8-byte integer real4 density
! standard 4-byte floating point real8
big_number ! 8-byte real
or
real (8) x !This declaration is compiler
dependent real (kind(0.d0)) y !Set y to the
same type as the double precision number 0.d0
8Implicit Declaration
If you DO NOT explicitly declare your variables
the compiler will implicitly declare them for you
Variables are implicitly declared by Names that
start with I-N are INTEGER Names that start with
A-H and O-Z are REAL
Implicit declaration is a common source of errors
You should always use IMPLICIT NONE !!
9Named Constants
Named constants have the PARAMETER attribute
real, parameter pi 3.14159
integer , parameter max_num 10
Can be used anywhere in the program when needed
circum 2. pi radius
10Named Constants
Advantages
- They reduce mistyping
- They can make formulas much clearer
- They make it easy to modify the program later
Expressions are allowed in named constants real,
parameter pi 3.14159, two_pi 2.pi
11Initialization
Initially variables start with undefined
values Can differ from run to run Can differ
from compiler to compiler
Initialization is similar to defining parameters
(without the parameter attribute)
real dx 0.1, dy 0.5
integer i 1, j 5, k 10
In the program the initializes values can change
dx dx 1.0
12Arithmetic Operators
5 built-in numeric operations
! addition operator c ab -
! subtraction c a-b !
multiplication c ab / !
division c a/b ! power
a b2
Exponents can be of any numeric type INTEGER,
REAL, or COMPLEX
13Examples of Arithmetic Expressions
A B C A B1 - C1 A B / 3.0 A B - C 3
N (A B) - C A (B - C) X 2 (A B) /
15.3 A B C
14Operator Precedence
Normal mathematical convention is used
- Operators bind according to precedence
- And generally from left to right
Precedence from highest to lowest is
power / multiplication and division -
addition and subtraction
Use parenthesis to control it!!
15Examples of Arithmetic Expressions
A B C lt---gt A B1 - C1 lt---gt A B
/ 3.0 lt---gt A B - C lt---gt 3 N
lt---gt (A B) - C lt---gt A (B - C)
lt---gt X 2 (A B) / 15.3 lt---gt A B
C lt---gt
A (B C) (A B1) - C1 A (B / 3.0) (A B) -
C 3 N (A B) - C A (B - C) (X 2) (A
B) / 15.3 A (B C)
16Integer Expressions
Expressions with integer constants and variables
INTEGER K, L, M N K (L1) / M3 - N
These are evaluated in integer arithmetic
In particular, division always truncates!!
K 4 and M 5 ----gt K M/2 is 6
and NOT 6.5
17Mixed Expressions
Expressions with integer and real constants and
variables
---gt INTEGER and REAL is evaluated as REAL
CAUTION (this can be deceptive)
REAL X 3. INTEGER K 5 X X
K/2
K/2 is still an INTEGER expression
18Type Conversions
Several ways can force a type conversion
---gt Intrinsic functions INT, REAL, and COMPLEX
We can also use appropriate constants
X X K / 2.0
X X (K 0.0) / 2
19Mixed Type Assignment
lt real variable gt lt integer expression gt
---gt RHS is evaluated and than converted to REAL
lt integer variable gt lt real expression gt
---gt RHS is evaluated and than truncated to
INTEGER
20Intrinsic Functions
Intrinsic functions are built-in functions that
you can just use
You dont need to declare them ---- just use them
Examples
X REAL (N) N INT (X) Y SQRT (X) Y SQRT
(1.5 SIN(X)2)
21Intrinsic Numeric Functions
REAL(N) ! Converts its argument n to
REAL INT(X) ! Truncates x to INTEGER AINT(X) !
The result remains REAL NINT(X) ! Converts x to
the nearest INTEGER ANINT(X) ! The result
remains REAL ABS(X) ! The absolute value of
the argument MAX(X,Y,) ! The maximum of its
arguments MIN(X,Y,) ! The minimum of its
arguments MOD(X,Y) ! X modulo Y
And many more
22Intrinsic Mathematical Functions
SQRT(X) ! Square root of X EXP(X) ! e raised to
the power X LOG(X) ! Natural logarithm of
X LOG10(X) ! Base 10 logarithm of X SIN(X) !
Sine of X, where X is in radians COS(X) ! Cosine
of X, where X is in radians TAN(X) ! Tangent of
X, where X is in radians ASIN(X) ! Arc sin of X
in radians ACOS(X) ! Arc cosine of X in
radians ATAN(X) ! Arc tangent of X in radians
And many more