AME 150 L - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

AME 150 L

Description:

REAL, INTEGER, LOGICAL constants & variables. CHARACTER ... ACHAR(I)or CHAR(I,kind) returns one character at position I in the ASCII collating sequence ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 20
Provided by: dickk
Category:
Tags: ame | achar

less

Transcript and Presenter's Notes

Title: AME 150 L


1
AME 150 L
  • Data Type and Structures

2
Fortran Data Types
  • Previously
  • REAL, INTEGER, LOGICAL constants variables
  • CHARACTER constants (not variables, yet)
  • Last lecture
  • Double Precision - REAL (Kind8)
  • Are there Others?

3
Single Precision
  • Single precision (32-bit) floating-point numbers
    use 8 bits in the exponent and 23 bits in the
    fraction, which gives about 7 digit precision and
    allows storage of numbers whose magnitudes range
    from roughly 10-38 to about 1038.

4
Double Precision
  • Double precision (64-bit) floating-point numbers
    use 11 bits in the exponent and 52 bits in the
    fraction, which gives about 15 digit precision
    and allows storage of numbers whose magnitudes
    range from roughly 10-308 to about 10308.

5
Fortran Double Precision
  • REAL (Kind8) list of variable
  • Machine dependent (not if use IEEE floating
    point)
  • (Kind4) is normal
  • Other Kinds
  • INTEGER has Kind1, 2, and 4
  • Logical has Kind1, 4

6
More Double Precision
  • Specifying Double Precision Constants
  • lot of decimal digits or"d" in exponent 1.D0 (is
    double precision 1) or _"kind" 1.0_8
  • Functions
  • Selected_Real_Kind( p, r )
  • Selected_Integer_Kind( r )
  • Kind(x) - returns kind for x
  • Precision(x) - returns precision for x
  • Range(x) - returns decimal exponent range for x

7
Complex Data Type
  • Follows rules of Complex arithmetic
  • z x i y, where i2 -1
  • alternate form z x i y r (cos? i sin ? )
  • where r (x2y2)1/2 and ? tan-1 (y/x)
  • Syntax
  • COMPLEX(Kind8 16),Dimension()
  • Complex stores a pair of REAL numbers as real
    part and imaginary part of a complex number

8
Complex (2)
  • Complex Constants and variables
  • Algebra z x i y
  • Fortran z (1.0, 0.25) constant
  • z CMPLX(Zr, Zi, Kind)
  • where Zr,Zi are real and imaginary parts, and
    Kind is (optional) the "kind" of result (8 or 16)

9
Complex (3)
  • Complex data type follows all rules
  • z1 z2 (z1r z2r) i (z1i z2i)
  • z1z2 z1r z2r - z1i z2i i (z1r z2i z1i z2r
    )
  • etc., and
  • zn ? rn cos (n ? ) i sin (n ? ), etc.
  • Note when complex numbers are input via READ
    statements, need (real, imag)

10
Complex (3)
  • Z CMPLX(x,y) ? x REAL(z) y AIMAG(z)
  • CABS(Z) is same as SQRT(Zr2Zi2)
  • Other functions do best they can
  • SIN, COS, SQRT, LOG, etc.
  • Use in Fortran
  • COMPLEX z1, z2, z(4,5)
  • z(1,j)z1z(2,k)-z2/z1 cos(z2)
  • Can use relational operators or /, but not
    others (gt, gt, lt, lt)

11
Overloading Operators
  • Note that definition of , -, , /, , etc. are
    modified for complex numbers (this is called
    overloading)
  • Other Overloads
  • REAL A(3), B(3), C(3)
  • C A B !Vector arithmetic
  • B A C etc.

12
Character Data Type
  • Attribute of character is len (for length)
  • Examples
  • CHARACTER(len5)alpha, beta, gamma
  • CHARACTER(5)delta
  • CHARACTERepsilon
  • alpha, beta, gamma, and delta are 5 characters
    long
  • epsilon is 1 character long

13
Character (2)
  • Manipulating CHARACTER variables
  • CHARACTER(8)c'abcdefghij',d
  • CHARACTER(4)a'abcd',b'1234'
  • d a // b !concatenation
  • d a(14) // '.' // b(24)
  • (xy) selects substring
  • (5) means from character 5 to end of string
  • (4) means from beginning to character 4
  • Can use relational operators (gt, lt, etc) carefully

14
Character (3)
  • There are two character ?Integer functions
  • ACHAR(I)or CHAR(I,kind) returns one character at
    position I in the ASCII collating sequence
  • IACHAR(char) or ICHAR(char) returns an Integer
    code for the character " char "
  • There are several Character String Utility
    Functions

15
Character (4)
  • Other Character Functions
  • ADJUSTL(String) Left Justify
  • ADJUSTR(String) Right Justify
  • REPEAT(String, Ncopies) Concatenate Ncopies of
    String
  • TRIM(String) Trim trailing blanks

16
Derived Data Types
  • Can create special data structures from Fortran's
    basic types integer, real, complex, logical,
    character
  • TYPE name of derived type
  • type declarations 1
  • type declarations 2 . . .
  • END TYPE name of derived type

17
Definition of Derived Type
  • TYPE person
  • CHARACTER(Len14)first_name
  • CHARACTER middle_initial
  • CHARACTER(Len14)last_name
  • CHARACTER(Len14)phone
  • INTEGER age . . .
  • END TYPE person

18
Use of Derived Type
  • TYPE (person)John, Jane
  • TYPE (person),Dimension(100)people
  • .. . .
  • Johnperson('John','R','Jones',
  • '323-1234',18,'M','123-45-6789')
  • Janeperson('Jane','C','Bass',
  • '765-4321',64,'F','987-65-4321')

19
More Use of Derived Type
  • Johnfirst_name "Milton"
  • Johnage Johnage 1
  • class(5)studentage 21
  • Class(j) refers to every element assigned to
    class
  • Any operation (and relational operators) can be
    custom defined
Write a Comment
User Comments (0)
About PowerShow.com