An Example Program - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

An Example Program

Description:

e.g 1, 21, -152, 34265, ... REAL for approximate fractional numbers ... AINT(X) ! The result remains REAL. NINT(X) ! Converts x to the nearest INTEGER ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 23
Provided by: cass76
Category:
Tags: aint | example | program

less

Transcript and Presenter's Notes

Title: An Example Program


1
An 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
2
Data 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 ,
3
Integers
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)
4
Integers
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
5
Real 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,
6
Complex 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)
7
Declaration 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
8
Implicit 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 !!
9
Named 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
10
Named 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
11
Initialization
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
12
Arithmetic 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
13
Examples 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
14
Operator 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!!
15
Examples 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)
16
Integer 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
17
Mixed 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
18
Type 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
19
Mixed 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
20
Intrinsic 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)
21
Intrinsic 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
22
Intrinsic 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
Write a Comment
User Comments (0)
About PowerShow.com