Title: 11'
111. 12. DERIVED OTHER DATA TYPES
-
- Ellis Philips, 1998, p 201-202
- p 297-334
- p335-358
2Derived data types
- Intrinsic data types integer, real, character,
logical, complex - F includes the capability for programmers to
create their own - data types to supplement the five intrinsic
types. A derived - type is defined by a special sequence of
statements. - type, public new_type
- component_definition
- .
- end type new_type
- The derived type definitions may only appear in a
module.
3- Example Suppose that the data includes first,
middle, initial and last - name, sex, social security number of individuals
- type, public person
- character (len12) first_name
- character (len1) middle_initial
- character (len12) last_name
- integer age
- character (len1) sex ! M or F
- character (len11) social_security
- end type person
- Declaration of variables
- type (person) jack, jill
4- Structure construct
- jackperson(Jack,R,Hagenbach,47,M,123-45-
6789) - jillperson(Jill,M,Smith,39,F,987-65-4321
) - A read statement will expect a sequence of data
values which - matches the components in both type and order.
- A print statement will output the value of a
derived type - variables as a sequence of its component parts.
- jilllast_namejacklast_name
- This statement changes the last_name of jill to
that of jack.
5- We may also use a derived data type in the
definition of another derived - data type.
- type, public employee
- type (person) employee
- character (len20) department
- real salary
- end type employee
-
- patemployeesexF
- This statement changes the sex of pat to F. is
used to obtain - a component of a derived type.
-
6- staff(i)pat
- staff is an array of type employee and pat is a
variable of the same type. - or
- staff(i)employeefirst_namepatemployeefirst_na
me - staff(i)employeemiddle_initialpatemployeemidd
le_initial - .
- .
- staff(i)salarypatsalary
- However, the operations between two objects of
the same derived type - are more difficult.
- patsalary tomsalary
- patdepartment tomdepartment
7- The derived data types of array_valued variables
- type, public golfer
- character (len15) first_name, last_name
- integer handicap
- integer, dimension (10) last_rounds
- end type golfer
- last_rounds is an array having bounds of 1 and 10
and will be used to - record the golfers 10 most recent scores.
- If a variable of type golfer is declared as
- type (golfer) Faldo
- then the score he achieved in his most recent
rounds is written as - Faldolast_rounds(10)
8- If the details of the members of a gulf club are
to be held in - an array whose elements are declared as
- type(golfer), dimension(250) member
- then the last name of an individual golfer will
be referred to as - member(i)last_name.
- member(j)last_rounds72
- sets every round for jth member to 72.
9Parameterized real variables
- Real numbers are stored in a computer as
floating-point - approximations to their true mathematical values.
- A real number is stored in a computer to about 6
or 7 - decimal digits of precision with an exponent
range of around - -1038 to 1038.
- An integer number can be in the range of -109 to
109. - Some computers such as super computers exceed
- these ranges considerably.
10- Overflow will occur if a calculation would result
in - an exponent for a real number being larger than
the maximum - possible exponent allowed. Overflow results in an
error - condition.
- 9876543210.1234 would require an exponent of 10
which is more than the computer will allow. - Underflow will occur if a calculation would
result in exponent - for a real number being smaller than the minimum
possible - exponent allowed. The result of underflow is that
the result of - the calculation is treated as zero. It is not
treated as an error - by many processor.
- 0.0000000000375
- This number requires an exponent of -10 which is
less than - the computer will allow.
11- To permit more precise control over the precision
and - exponent range of floating-point numbers, real
variables are - parameterized. They have a parameter associated
with them - that specifies minimum precision and exponent
range - requirements.
-
- real a,b
- real c,d
- real, dimension (10) x,y
- integer, parameter kind11, kind24, kind32
- real (kindkind2) e,f
- real (kindkind1) g,h
- real (kindkind3), dimension(10) u,v
12- The value of kind must be a named integer
constant. - real (kind4) e,f !not allowed
- It appears that no portability has been gained,
since - a variable of kind type 2 may have 14 significant
- digits of precision and an exponent range of 100
on one - machine while it has six digits of precision and
exponent - range of 30 on another.
- A real variable or constant whose kind type
parameter is not - specified, is of default real type.
13The selected_real_kind intrinsic function
- The selected_real_kind intrinsic function may be
used to determine - the kind type parameter of the real number
representation on the current - processor which meets, at least, a specified
degree of precision and - exponent range.
- real(kindselected_real_kind (p8, r30)) m
- real(kindselected_real_kind (p6, r30)) n
- The selected_real_kind intrinsic function has 2
optional arguments. - p is a scalar integer argument specifying the
minimum number of - decimal digits required, and r is a scalar
integer argument specifying - the minimum decimal exponent range required.
- The result of the selected_real_kind function is
the kind type that meets - or minimally exceeds the requirements specified
by p and r. - Most computers have provision to store
floating-point numbers using one - of two precisions, usually referred to as
single-precision and double - precision with corresponding hardware registers
to perform arithmetic
14Complex variables
- A complex number is generally written as abi
where a and - b are real numbers and i called the imaginary
unit, has - the property that i2-1. The real numbers a and b
are called - the real and imaginary parts of abi
respectively. - Addition of complex numbers
- (abi)(cdi)(ac)(bd)i
- Substraction of complex numbers
- (abi)-(cdi)(a-c)(b-d)i
- Multiplication of complex numbers
- (abi)(cdi)(ac-bd)(adbc)i
- Division of complex numbers
15- Intrinsic data types integer, real, character,
logical, - complex
- complex name1,name2
- (1.5,7.3)
- (1.59e4,-12e-1)
- Intrinsic functions
- real (z) where z is complex, delivers the real
part of z - aimag (z) where z is complex, delivers the
imaginary part of z - cmplx (a) if a is real, delivers the complex
value (a,0.0) if a is integer, delivers the
complex value (real (a), 0.0) - if a is complex, delivers the complex value a
- cmplx (x, y) where x and y must be integer or
real, delivers - the complex value (real (x), real (y))
16- conjg (z) where z is complex, delivers the
complex conjugate(x,-y) where x and y are the
real - and imaginary parts of z, respectively.
- All of the generic intrinsic numeric functions
such as sin, log - and so on can also be used with complex arguments.
17Specifying non-default kinds of variables and
constants
- The use of parameterized real variables and
constants in - conjunction with the selected_real_kind intrinsic
function, - provides a portable means of specifying the
precision and - exponent range for numerical algorithms.
- type (kindkind_type) list of variable names
-
- type is one of integer, real, complex or
logical. - kind_type must be a scalar named integer
constant which has a non negative value.
18- Default kind type declarations
- real x,y
- complex, dimension (n) z
- integer, parameter i25
- logical danger
- Explicit kind type declarations
- integer, parameter k22,k33,k55, k1010
- real (kindk2) x, y
- complex(kindk3), dimension(n) z
- integer (kindk10), parameter i25
- logical (kindk5) danger
19- Since variables can be of different kinds, it
follows that the same must also apply to - constants.
- Integer literal constants
- 628 default integer
- -628_small integer of kind small
- 628_large integer of kind large
- where small and large are scalar named integer
constants whose values are non-negative. - Real literal constants
- -1.0 default real
- 12.34 default real
- 401.2e-5 default real
- -314.2e-3_low real of kind low
- 704.2e-3_high real of kind high
- Complex literal constants
- (1.5,2.7) default complex
20- The intrinsic functions selected_int_kind and
selected_real_kind enable - precision and range requirements to be specified
in a portable fashion. - Example Using a module to specify the kind types
of integer variables and - constants
- module kind_types
- integer, parameter, public rangeselected_int_
kind (20) - end module kind_types
- program degree
- use kind_types
- integer (kindrange) x, y, z
- x360_range
- y180_range
- z90_range
- ..
- end program degree
21- integer, parameter real_kindselected_real_kind
(p,r) - This statement sets the constant real_kind to a
kind type parameter for a real - data type that has at least p digits of accuracy
and a decimal exponent range of - at least r.
- Example Specifying the precision and range of
real variables and constants - module kind_types
- integer, parameter, public real_kindselected_
real_kind (6, 30) - end module kind_types
- program satellite
- use kind_types
- real(kindreal_kind) r, theta, phi
- r321.172_real_kind
- theta1.47239_real_kind
- phi0.17234e-1_real_kind
- .
- end program satellite
22Example Specifying the precision and range of
complex variables and constants.
- module kind_types
- integer, parameter, public complex_kindselecte
d_real_kind(12,70) - end module kind_types
- program accurate
- use kind_types
- complex (kindcomplex_kind), dimension(4) z
- z(1)(3.7247177e-45_complex_kind,
723.115798e-56_complex_kind) - .
- end program accurate
- This program fragment declares an array of
complex numbers of whose - elements has real and imaginary parts having at
least 12 digits of - precision and an exponent range of at least 70.
23Mixed kind expressions
- The intrinsic function int (a, kind_type)
converts the argument - a to an integer, a can be integer, real or
complex and can be - scalar or array_valued.
- jjint (x, kind(j))
- This converts the value of the variable x to an
integer of - the same kind as the (integer) variable j before
adding to j - and storing the result in j.
- In similar way, the intrinsic function real
(a,kind_type) - converts the argument a to real, a can be
integer, real or - complex and be scalar or array valued.
24- integer, parameter kselected_int_kind (10)
- integer, parameter pselected_real_kind (8)
- integer(kindk) i, j
- real(kindp) x
- The integer i potentially has 10 decimal digits
of accuracy. - When it is converted to a real with a requirement
for six - decimal digits of accuracy, therefore, it must be
assumed - that some loss of precision may result.
25Intrinsic functions which have an optional kind
argument
- Function name arguments Purpose
- aint (a,kind) Truncation
- anint (a,kind) Nearest whole number
- cmplx (x,y,kind) Convert to complex
- int (a,kind) Convert to integer
- logical (l,kind) Convert between kinds of
logical - nint (a,kind) Nearest integer
- real (a,kind) Convert to real
26Example Define a data type for complex numbers,
read two complex numbers, calculate their sum,
difference and product, print the results.
- module complex_arithmetic
- type, public complex_number
- real real_part, imaginary_part
- end type complex_number
- end module complex_arithmetic
- program complex_example
- use complex_arithmetic
- type(complex_number) c1, c2, csum, sdiff,
cprod - read ,c1,c2
- csumreal_partc1real_partc2real_part
- csumimaginary_partc1imaginary_partc2imaginary
_part - cdiffreal_partc1real_part-c2real_part
- cdiffimaginary_partc1imaginary_part-c2imaginar
y_part
27In Class Problem Session -11
- Objective Learning programming of operations
requiring the complex numbers - Assignment Write a program to solve the
following quadratic equation - ax2bxc0 where a1, b2, c5.
- The program should read the complex coefficients
a, b and c of - a quadratic equation, use the quadratic formula
to calculate the roots and - display them as complex numbers.
-