Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Computing Fundamentals-I

FORTRAN 77: Data Types

Dr. Rizwan Ahmed


Department of Nuclear Engineering
Pakistan Institute of Engineering and Applied Sciences
(PIEAS), P. O. Nilore, Islamabad
Contact: 2207381-5 (3750)
E-mail: rizwanahmed@pieas.edu.pk, rahmed.ne@gmail.com
Data

 FORTRAN Character Set


 Numbers
 0, 1, 2, 3, 4, 5, 6, 7, 8 & 9
 Alphabets
 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W,
X, Y, & Z
 Characters
 ' " ( ) * + - = / blank : ! & $ ; < >
% ? , .
 Extension to FORTRAN 77
 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, & z
Anyone can Program like anyone can cook!

You must be imaginative,


strong hearted, you must try
things that may not work.
And you should not let any
one to define your limits
from where you have come
from.

Anyone can program, but


the fearless can be great.
Storing Data

Variables:
Variables are means to
store the data.

By using variables we can


store and manipulate 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.

 FORTRAN is case insensitive.


Data Types

 Fortran can deal with six data types


 Numerical
 Real
 Double precision
 Integers
 Complex
 Non-Numerical
 Character
 Logical
Variable Declaration

TYPE Vname1, Vname2, …….

• TYPE refers to real, integer, double precision, complex,


character, logical
• Vname refers to the name of variable like a, b, LHS, RHS,
i_count, Height, time, and distance etc.

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.

2. Be cautious about your calculations while using integers


and reals together.
Data Types (Constants)

 REAL (Floating point)


 Real constants written in decimal form must contain a
decimal point.
 +10.625, -6.2, 5.0, 0.0, +6.7
 Exponential Notation
 Real data can be represented in decimal form as
exponents.
 1.65E3 (1.65×103), -2.32e-3 (-2.32×10-3), 6.23e23 (6.23×1023)

! 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

 The default precision for real data is typically about seven


decimal digits and the range around -10-38 to 1038
(although this is machine dependent).
Data Types (Constants)

 REAL (Double precision)


 Double precision constants are accurate to 14 to 16 decimal places,
depending on the machine.

! Declaration
DOUBLE PRECISION A, B, C

 0.0D0 (0.0), -0.23D-94 (-0.23×10-94), 6.23e23 (6.23×1023)

 CAUTION
 Double precision numbers require two to ten times the
computational time compared to single precision numbers.
Data Types (Constants)

 REAL (DOUBLE PRECISION)


 Fortran uses 64 bits for a DOUBLE PRECISION number
 1 bit for sign
 52 bits for mantissa
 11 for exponent

 The largest number represented by double precision is about


10308.

 There are double precision functions for trigonometric,


hyperbolic, logarithmic etc. calculations.
Data Types (Example no. 2)
Data Types (Example no. 3)
Data Type (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)

! Declare AA as a character variable

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

 Find time when it is at the height of 5 m for the second time?

You might also like