Professional Documents
Culture Documents
Lec-04 Fortran Data Types
Lec-04 Fortran Data Types
Variables:
Variables are means to
store the data.
Note:
a, b, c, d, e, f and g are
variables.
Data Types
Variables
Variables are means to store the data.
By using variables, we can store and manipulate the data.
Variable names
The variables names may contain one to six (Fortran 90
allows up-to 32) characters consisting of both alphabetic
characters and digits; however, the first character must
be alphabetic.
Note:
All variable names starting from I,J,K,L,M,N are considered as
INTEGERS by default.
Data Types (constants)
INTEGER
Integers are positive or negative whole numbers
(including 0) represented by a string of numbers not
including commas or decimal points.
Integers can be preceded by a negative (-) sign
Examples: 5,-10,+25, -26351
! Declaration
INTEGER A, B
Data Types (constants)
INTEGER
FORTRAN uses 32 bits for an INTEGER.
Typical range of values represented on a computer is from
-232-1 (-2147483648) to 232-1-1 (2147483647).
Note:
Exceeding the range leads to integer overflow or
underflow
Data Types (Example no. 1)
Data Types (Example no. 1) cont’d
D:\computing\Fortan-g77\g77\bin>g77 integer_type.for -o
integer_type.exe
D:\computing\Fortan-g77\g77\bin>integer_type
dis= 200
time= 10
V= 20
value of I= -9i_count= -4
X= 25Y= 62
Note:
1. Check the value of ‘time’, ‘V’, ‘i_count’ and ‘Y’. The integer
value is returned.
! Declaration
REAL A, B, C
Data Types (Constants)
REAL
Fortran uses 32 bits for a REAL number
1 bit for sign
23 bits for mantissa
8 bits for exponent
! Declaration
DOUBLE PRECISION A, B, C
CAUTION
Double precision numbers require two to ten times the
computational time compared to single precision numbers.
Data Types (Constants)
Complex
Complex are declared with their first value as the real
component and the second as the imaginary.
There are double precision functions for trigonometric,
hyperbolic, logarithmic etc. calculations.
! Declaration
COMPLEX A
! Set the value of A to 4.2 3.1i
A = (4.2, 3.1)
Data Types (Example no. 3)
i 1
A 4.2 3.1i
B 3.2 2.1i
Data Types (Example no. 3)
Data type (Character)
Character
Character strings are a sequence of Fortran symbols
enclosed within quotes(“) or apostrophes (‘)
The number of characters in a sequence is the length.
Blanks are counted as characters.
By default, the length of any character string is one.
Data Types (Character)
CHARACTER*10 AA
CHARACTER (10) AA, BB
! AA and BB are character variables
! can store 10 alphabets
! Set AA equal to the name say, PIEAS
AA=“PIEAS“
BB=‘FELLOW’
Note:
Length of character must be specified with the word
Character.
Data type (Example no. 4)
Data type (Example no. 4)
Data Types (Logical)
Logical
Logical variables have one of two values: .TRUE. or .FALSE.
Logical values can be displayed but are represented by only
a T or and F, preceded by a space.
! Declare C & D as a LOGICAL variables
LOGICAL C,D
! C and D are Logical variables
! Set B equal to false and D equal to true
C=.FALSE.
D=.TRUE.
Data Type (Example no. 5)
Data Type (Example no. 5)
Data Type conversion Functions
Function Result
DBLE (x) Converts a real/integer to double
precision
FLOAT(J) Converts an integer to real
INT(x) Converts a real to integer
ICHAR(A) Converts the first character stored in
character variable to its ASCII integer
CHAR(I) Converts the integer to its ASCII character
COMPLEX or CMPLX Converts a real number to a complex
number
REAL (CX) Gets the real part of a complex number
IMAG(CX) Gets the imaginary part of complex
number
Parameters
PARAMETERIZATION
Once a constant is declared as a parameter it can no
longer be changed.
! Declaration
REAL PI
PARAMETER (PI=3.141593) ! PI is real & fixed
REAL G
PARAMETER (G=6.67E-11) ! Gravitational constant
INEGER N
PARAMETER (N=10) ! N is fixed
Example
Projectile Motion
1 2
x vot cos , y vot sin gt
2
10
9
Vo=20 m/s 8
Θ=60° 7
6
g=9.81 m/s2 5
Y
4
Δt=0.05 s 3
2
1
0
0 2 4 6 8 10 12 14 16 18 20
X