1 / 16

Mixing Objects of Different Types

Mixing Objects of Different Types. Mixed Type Numeric Expressions Mixed Type Assignment Integer Division. Mixing Objects of Different Types . Mixed Type Numeric Expressions

gaius
Download Presentation

Mixing Objects of Different Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mixing Objects of Different Types • Mixed Type Numeric Expressions • Mixed Type Assignment • Integer Division

  2. Mixing Objects of Different Types • Mixed Type Numeric Expressions • In the CPU calculations must be performed between objects of the same type, so if an expression mixes type some objects must change type. • Default types have an implied ordering: • INTEGER -- lowest • REAL • DOUBLE PRECISION • COMPLEX -- highest • The result of an expression is always of the highest type, for example, • INTEGER * REAL gives a REAL, (3*2.0 is 6.0) • REAL * INTEGER gives a REAL, (3.0*2 is 6.0) • DOUBLE PRECISION * REAL gives DOUBLE PRECISION, • COMPLEX * < anytype > gives COMPLEX, • DOUBLE PRECISION * REAL * INTEGER gives DOUBLE PRECISION. • The actual operator is unimportant.

  3. Mixing Objects of Different Types • Mixed Type Assignment • Problems often occur with mixed-type arithmetic; the rules for type conversion are given below. • INTEGER = REAL (or DOUBLE PRECISION) • The RHS is evaluated, truncated (all the decimal places lopped off) then assigned to the LHS. • REAL (or DOUBLE PRECISION) = INTEGER • The RHS is promoted to be REAL and stored (approximately) in the LHS. • For example, • REAL :: a = 1.1, b = 0.1 • INTEGER :: i, j, k • i = 3.9 ! i will be 3 • j = -0.9 ! j will be 0 • k = a - b ! k will be 1 or 0 • Note: as a and b stored approximately, the value of k is uncertain.

  4. Mixing Objects of Different Types • Integer Division • Confusion often arises about integer division; in short, division of two integers produces an integer result by truncation (towards zero). Consider, • REAL :: a, b, c, d, e a = 1999/1000 ! LHS is 1 b = -1999/1000 ! LHS is -1 c = (1999+1)/1000 ! LHS is 2 d = 1999.0/1000 ! LHS is 1.999 e = 1999/1000.0 ! LHS is 1.999 • a is (about) 1.000 • b is (about) -1.000 • c is (about) 2.000 • d is (about) 1.999 • e is (about) 1.999 • Great care must be taken when using mixed type arithmetic.

  5. Intrinsic Procedures • Intrinsic Procedures • Type Conversion Functions • Mathematical Intrinsic Functions • Numeric Intrinsic Functions • Character Intrinsic Functions

  6. Intrinsic Procedures • Intrinsic Procedures • Fortran 90 has 113 in-built or intrinsic procedures to perform common tasks efficiently, they belong to a number of classes: • elemental such as: • mathematical, for example, SIN or LOG. • numeric, for example, SUM or CEILING; • character, for example, INDEX and TRIM; • bit, for example, IAND and IOR; • inquiry, for example, ALLOCATED and SIZE; • transformational, for example, REAL and TRANSPOSE; • miscellaneous (non-elemental SUBROUTINE s), for example, SYSTEM_CLOCK and DATE_AND_TIME. • Note all intrinsics which take REAL valued arguments also accept DOUBLE PRECISION arguments.

  7. Intrinsic Procedures • Type Conversion Functions • It is easy to transform the type of an entity, • REAL(i) converts i to a real approximation, • INT(x) truncates x to the integer equivalent, • DBLE(a) converts a to DOUBLE PRECISION, • IACHAR(c) returns the position of CHARACTER c in the ASCII collating sequence, • ACHAR(i) returns the character in the ASCII collating sequence. • All above are intrinsic functions. For example, • PRINT*, REAL(1), INT(1.7), INT(-0.9999) • PRINT*, IACHAR('C'), ACHAR(67) • are equal to • 1.000000 • 1 0 67 C

  8. Intrinsic Procedures • Mathematical Intrinsic Functions • Summary,

  9. Intrinsic Procedures Numeric Intrinsic FunctionsSummary,

  10. Intrinsic Procedures • Character Intrinsic Functions • Summary,

  11. More Intrinsic Functions • Random Number Intrinsic • Vector and Matrix Multiply Intrinsics • Maximum and Minimum Intrinsics • Array Location Intrinsics • Array Reduction Intrinsics • Array Reduction Intrinsics (Cont'd)

  12. More Intrinsic Functions • Random Number Intrinsic • RANDOM_NUMBER(HARVEST) will return a scalar (or array of) pseudorandom number(s) in the range . • For example, • REAL :: HARVEST • REAL, DIMENSION(10,10) :: HARVEYS CALL RANDOM_NUMBER(HARVEST) • CALL RANDOM_NUMBER(HARVEYS) • RANDOM_SEED([SIZE=< int >]) finds the size of the seed. • RANDOM_SEED([PUT=< array >]) seeds the random number generator. • CALL RANDOM_SEED(SIZE=isze) CALL RANDOM_SEED(PUT=IArr(1:isze))

  13. More Intrinsic Functions • Vector and Matrix Multiply Intrinsics • There are two types of intrinsic matrix multiplication: • DOT_PRODUCT(VEC1, VEC2) -- inner (dot) product of two rank 1 arrays. For example, • DP = DOT_PRODUCT(A,B) is equivalent to: • DP = A(1)*B(1) + A(2)*B(2) + ... For LOGICAL arrays, the corresponding operation is a logical .AND.. • DP = LA(1) .AND. LB(1) .OR. & LA(2) .AND. LB(2) .OR. ... • MATMUL(MAT1, MAT2) -- `traditional' matrix-matrix multiplication: • if MAT1 has shape (n, m) and MAT2 shape (m, k) then the result has shape (n, k); • if MAT1 has shape (m) and MAT2 shape (m, k) then the result has shape (k); • if MAT1 has shape (n, m) and MAT2 shape (m) then the result has shape (n); • For LOGICAL arrays, the corresponding operation is a logical .AND..

  14. More Intrinsic Functions • Maximum and Minimum Intrinsics • There are two intrinsics in this class: • MAX(SOURCE1,SOURCE2[,SOURCE3[,...]]) -- maximum values over all source objects • MIN(SOURCE1,SOURCE2[,SOURCE3[,...]]) -- minimum values over all source objects • Scan from left to right, choose first occurrence if there are duplicates • MAX(1,2,3) is 3 • MIN((/1,2/),(/-3,4/)) is (/-3,2/) • MAX((/1,2/),(/-3,4/)) is (/1,4/)

  15. More Intrinsic Functions • Array Location Intrinsics • There are two intrinsics in this class: • MINLOC(SOURCE[,MASK]) -- Location of a minimum value in an array under an optional mask. • MAXLOC(SOURCE [,MASK]) -- Location of a maximum value in an array under an optional mask. • A 1D example, • A 2D example. If • then • MINLOC(Array) is (/3,4/) • MAXLOC(Array,Array.LE.7) is (/1,4/) • MAXLOC(MAXLOC(Array,Array.LE.7)) is (/2/) (array valued).

  16. More Intrinsic Functions • Array Reduction Intrinsics • PRODUCT(SOURCE[,DIM][,MASK]) -- product of array elements (in an optionally specified dimension under an optional mask); • SUM(SOURCE[,DIM][,MASK]) -- sum of array elements (in an optionally specified dimension under an optional mask). • The following 1D example demonstrates how the 11 values are reduced to just one by the SUM reduction: • Consider this 2D example, if • PRODUCT(A) is 720 • PRODUCT(A,DIM=1) is (/2, 12, 30/) • PRODUCT(A,DIM=2) is (/15, 48/)

More Related