BIL106E - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

BIL106E

Description:

Prelude. There are three data types provided in F language that we have not yet considered: ... PRELUDE. DRIVED DATA TYPES. Definition. Declaration. Use ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 35
Provided by: insIt6
Category:
Tags: bil106e | prelude

less

Transcript and Presenter's Notes

Title: BIL106E


1
BIL106E
  • Drived OtherDATA TYPES

2
Contents
  • PRELUDE
  • DRIVED DATA TYPES
  • Definition
  • Declaration
  • Use
  • PARAMETERIZED TYPES
  • COMPLEX TYPE

3
Prelude
  • There are three data types provided in F language
    that we have not yet considered
  • Derived data type
  • Parameterized types
  • Complex type

4
  • PRELUDE
  • DRIVED DATA TYPES
  • Definition
  • Declaration
  • Use
  • PARAMETERIZED TYPES
  • COMPLEX TYPE

5
Derived Data Types
  • There are intrinsic data types (integer, real,
    character, logical and complex) in F Language.
  • F includes the capability for programmers to
    create their own data types to supplement the
    five intrinsic types.
  • They can be derived from
  • Intrinsic data types integer, real, character,
    logical, complex
  • Previously defined data types

6
Derived data types and structures
  • F provides a "derived data type" in order to
    declare a "structure" used to store items of
    different types.
  • The position in the structure, in which these
    data items are stored, are called the
    "components of the structure".
  • Generic form of a derived type definition
  • type, public type-name
  • variable declaration_1
  • variable declaration_2
  • ..
  • end type type-name

type, public tel_record character (len15)
name character (len25) surname integer
tel-no end type tel_record
7
Definition Declaration
  • Suppose that the data includes first, middle,
    initial and last name, sex, social security
    number of individuals
  • Definition of the derived type person
  • 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 of derived type person
  • type (person) jack, jill

8
Definition Declaration
  • The derived type definitions may only appear in a
    module.
  • The other modules can gain access to DT with
    public attribute in type definition

type, private type-name variable
declaration_1 variable declaration_2 .. end type
type-name
type, public type-name variable
declaration_1 variable declaration_2 .. end type
type-name
9
Use
  • We put constant data in a derived type variable
    using Structure Constructor form
  • jackperson("Jack","R","Hagenbach",47,"M","123-45-
    6789")
  • jillperson("Jill","M","Smith",39,"F","987-65-4321
    ")
  • Reffering to componentsvariablecomponent
  • structure_namecomponent_name
  • Examplejackfirst_name "Jack"jillage
    modified_age

10
Use
  • 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.

11
Derived types in derived types
  • type, public employee type(person)
    employee_pers character(len20) department
    real salaryend type employee
  • type(person) john
  • johnemployee_persage 34johndepartment
    "geotechnical engineering"

12
Arrays of derived types
  • type(person), dimension(136) bil106class
  • Thenbil106class(2)sex "M"
  • bil106class(i) jack
  • You can do operations on componentsbil106class(3
    )agebil106class(4)agename_surname jackname
    // jacksurname
  • But no such operation is permissiblebil106class(
    3) - bil106class(4)

13
Arrays in derived types
  • 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
  • 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)

14
  • PRELUDE
  • DRIVED DATA TYPES
  • Definition
  • Declaration
  • Use
  • PARAMETERIZED TYPES
  • COMPLEX TYPE

15
Parameterized Types
  • 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.

16
  • 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.

17
  • 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.

18
  • Real variables may be parameterized in order to
    provide more than one representation of real
    numbers, with differing degrees of precision or
    exponent range.
  • A processor which supports more than one
    representation of real numbers will specify a
    different kind type parameter for each
    representation.

19
  • For computations in which more precision is
    needed than is available using the default real
    data type. Fortran provides parameterized real
    types. A parameterized type declaration has the
    form
  • type-specifier (KIND kind-number), attributes
    list
  • or
  • type-specifier (kind-number), attributes list
  • where kind-number is an integer constant or a
    constant integer expression whose value is
    positive. Kind numbers, called kind type
    parameters, for various types are machine
    dependent.

20
  • There must be at least two kinds of real types,
    one for single-precision values and other for
    double-precision values, which provides
    approximately twice as many significant digits as
    single precision. The kind numbers for these two
    types typically are 1 and 2
  • Real kind 1 Single-precision values with
    approximately 7 significant digits usually
    stored in 32 bits (default real type)
  • Real kind2 Double-precision values with
    approximately 14 significant digits usually
    stored in 64 bits

21
  • 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

22
  • The value of kind must be a named integer
    constant.
  • real (kind4) e,f !not allowed
  • Example A variable of kind type2 may have 14
    significant digits of precision and an exponent
    range of 100 on one machine.
  • A real variable or constant whose kind type
    parameter is not specified, is of default real
    type.

23
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 argument specifying the minimum
    number of decimal digits required,
  • 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.

24
  • Example
  • real (kind selected_real_kind (20, 50)) X
  • declares that X is to be a real variable whose
    values are to have at least 20 decimal digits of
    precision and may be in the range -1050 to 1050.

25
  • A reference to selected_int_kind has the form
  • selected_int_kind (r)
  • where r is an integer. It returns a kind type
    parameter that will provide a range of at least -
    10r to 10r, if such a kind is available
    otherwise it returns -1.
  • integer, parameter Range20
    selected_int_kind(20)
  • integer (kind Range20) M, N
  • declare that Mand N are integer variables whose
    values may have up to 20 digits. If no such
    integer kind is available, a compiler error will
    result.

26
  • To specify the kind of a constant, an underscore
    followed by a kind number is appended to the
    constant. For example
  • 123456789_
  • is an integer constant whose kind number is 3,
    and
  • 12345678901234567890_Range20
  • is an integer constant whose kind number is
    Range20, where Range20 is the named constant
    defined earlier by
  • integer, parameter Range20 selected_int_kind(2
    0)

27
  • When a value is assigned to a parameterized real
    variable in a program, it is important that the
    kind of the value being assigned is the same as
    the kind of the variable. Consider the following
    program
  • program test
  • implicit none
  • real X
  • integer, parameter DP selected_real_kind(14)
  • real (kind DP) A, B
  • X0.1
  • B0.1_DP
  • AX
  • print , A
  • A B
  • print , A
  • end program test

28
  • On some systems the values displayed for A by
    the first two print statements resemble the
    following
  • 0.1000000014901161
  • 0.100000000000000
  • It is important to ensure that all variables,
    arrays, and functions that areto have values of a
    particular kind are declared to be of that kind.
    For example, if R is a real variable of kind DP
    and Area is an ordinary (single-precision) real
    variable, the computation in the statement
  • Area 3.1415926535898_DP R 2
  • will be carried out in extended precision, but
    the resulting value will then be assigned to the
    single-precision variable Area, thus losing
    approximately half of the significant digits.

29
  • As these examples illustrate, although mixed-kind
    expressions are permitted, accuracy may be lost
    because of the use of lower-precision constants
    or variables.

30
  • PRELUDE
  • DRIVED DATA TYPES
  • Definition
  • Declaration
  • Use
  • PARAMETERIZED TYPES
  • COMPLEX TYPE

31
Complex Type
  • A complex number is a number of the form abi
    where a and b
  • are real numbers. The first real number, a, is
    called the real part of
  • the complex number, and the second real number,
    b, is called the
  • imaginary part.
  • In Fortran, a complex constant is represented as
    a
  • pair of real constants.
  • (a, b)
  • where a and b represent the real part and the
    imaginary part of the complex number,
    respectively.
  • For example
  • (1.0, 1.0) ? 1.0 1.0 i
  • (-6.0, 7.2) ? -6.0 7.2i
  • (-5.432, -1.4142) ? -5.432 -1.4142i

32
  • The names of variables, arrays, or functions that
    are complex may be any legal Fortran names, but
    their types must be declared using the complex
    type statement. For example, the statements
  • complex A, B
  • complex, dimension (10, 10) Rho
  • declare A, B, and the 10 X 10 array Rho to be
    complex variables.

33
  • The statements
  • integer, parameter DP selected_real_kind(14)
  • complex(kind DP) Gamma
  • or
  • integer, parameter DP selected_real_kind(14)
  • complex(DP) Gamma
  • declare the variable Gamma whose type is complex
    of kind DP. For a complex value, this means that
    both the real part and the imaginary part are
    real values of kind DP (that is, both are
    double-precision values).

34
Example
  • 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_part c1imaginary_part
    c2imaginary_part
  • cdiffreal_part c1real_part
    - c2real_part
  • cdiffimaginary_part c1imaginary_part -
    c2imaginary_part
  • cprodreal_part c1real_part
    c2real_part -
  • c1imaginary_part c2imaginary_part
  • cprodimaginary_part c1real_part
    c2imaginary_part
  • c1imaginary_part c2real_part
  • print , csum, cdiff, cprod
  • end program complex_example
Write a Comment
User Comments (0)
About PowerShow.com