other data types. standard type sizes b most machines store integers and reals in 4 bytes (32 bits)...

44
Other data types Other data types

Upload: elvin-sharp

Post on 05-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Other data typesOther data types

Page 2: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Standard type sizesStandard type sizes

Most machines store integers and Most machines store integers and reals in 4 bytes (32 bits)reals in 4 bytes (32 bits)

Integers run from -2,147,483,648 Integers run from -2,147,483,648 to 2,147,483,647to 2,147,483,647

This is 2This is 23232

Single precision reals run from -Single precision reals run from -10103838 to 10 to 103838

Page 3: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Representational problemRepresentational problem

Arithmetic errors are possible whenever Arithmetic errors are possible whenever real numbers are used in computing.real numbers are used in computing.

This is because only a limited number of This is because only a limited number of bits are available to represent the bits are available to represent the exponent and mantissa of a real number.exponent and mantissa of a real number.

The results are called The results are called • overflow errors, andoverflow errors, and• underflow errorsunderflow errors

Page 4: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Roundoff errorRoundoff error

roundoff error occurs whenever a roundoff error occurs whenever a real number must be real number must be approximated to fit into the alloted approximated to fit into the alloted space.space.

For example: For example:

(A+B)2 - 2AB - B2

A2 + 2AB + B2 - 2AB + B2

A2

A2

A2

A2

=

=

Page 5: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Roundoff errorRoundoff error

The result should be 1, but may The result should be 1, but may not turn out that way due to not turn out that way due to rounding errors.rounding errors.

(A+B)2 - 2AB - B2

A2 + 2AB + B2 - 2AB + B2

A2

A2

A2

A2

=

= = 1?

Page 6: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Solution?Solution?

One way to deal with rounding One way to deal with rounding error is to increase the size error is to increase the size (number of bits) used to represent (number of bits) used to represent your data.your data.

Instead of the defaults, we Instead of the defaults, we ‘parameterize’ the data types...‘parameterize’ the data types...

Page 7: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Parameterized typesParameterized types

A parameterized type is one that can be A parameterized type is one that can be used to construct larger configurations used to construct larger configurations of a native typeof a native type

You use parameterized types when you You use parameterized types when you suspect that the size of a conventional suspect that the size of a conventional native type will not accommodate the native type will not accommodate the size of the values you could encounter.size of the values you could encounter.

You also use parameterized types to You also use parameterized types to reduce the size of a representation.reduce the size of a representation.

Page 8: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Parameterized examplesParameterized examples

REAL(KIND = 1) :: num1

REAL(KIND = 2) :: num2

32 bits

64 bits

Double precision

Single precision

Page 9: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Parameterized examplesParameterized examples

INTEGER(KIND = 1) :: num1

INTEGER(KIND = 4) :: num4

8 bits

64 bits

INTEGER(KIND = 3) :: num3

32 bits

INTEGER(KIND = 2) :: num216 bits

-27 - (27 - 1)

-215 - (215 - 1)

-231 - (231 - 1)

-263 - (263 - 1)

Page 10: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Functions for Functions for parameterized typesparameterized types

There are a series of functions that There are a series of functions that allow us to establish and allow us to establish and manipulate data of parameterized manipulate data of parameterized typestypes

These functions are unique to the These functions are unique to the Fortran90 languageFortran90 language

Page 11: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

SELECTED_REAL_KIND(p,r)SELECTED_REAL_KIND(p,r)

This function returns the KIND type that is necessary fora real number to store a value with p decimal digits ofprecision within the range of r, where r is a power of 10.

Example:To store the value 1.23456789012345678901234567890we need 29 decimal digits (precision)

SELECTED_REAL_KIND(29,3)for values between -1,000 and 1,000

REAL(KIND = SELECTED_REAL_KIND(29,3)) :: num

Page 12: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

SELECTED_SELECTED_INTINT_KIND(r)_KIND(r)

This function returns the KIND type that is necessary for an integer number to store a value within range r such that 10-r <= num <= 10r

Example:SELECTED_INT_KIND(9)for values between -1,000,000,000 and 1,000,000,000

INTEGER(KIND = SELECTED_INT_KIND(9)) :: num

Page 13: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

PRECISION(num)PRECISION(num)

This function returns the number of decimal digits ofprecision for a variable.

Example:To store the value 1.23456789012345678901234567890we need 29 decimal digits (precision)

REAL(KIND = SELECTED_REAL_KIND(29,3)) :: num

PRINT*, “The precision of num is: “, PRECISION(num)

Page 14: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

RANGE(num)RANGE(num)

This function returns the range of the exponents fora given value.

Example:

REAL(KIND = SELECTED_REAL_KIND(29,3)) :: num

PRINT*, “The range of num is from 10^-”, RANGE(num),& “- 10^”, RANGE(num)

Page 15: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Other native data typesOther native data types

Complex numbersComplex numbers• contain both a real and imaginary contain both a real and imaginary

partpart CharactersCharacters

• used primarily for character stringsused primarily for character strings• we have already made use of some of we have already made use of some of

these.these.

Page 16: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

type COMPLEXtype COMPLEX

COMPLEX numbers have both a COMPLEX numbers have both a real and imaginary part (a + bi) real and imaginary part (a + bi) where i is the square root of where i is the square root of negative 1.negative 1.

COMPLEX is a native type in COMPLEX is a native type in FORTRAN 90 but not in earlier FORTRAN 90 but not in earlier versions of the language.versions of the language.

Page 17: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Operator overloadingOperator overloading

Since COMPLEX number addition Since COMPLEX number addition does not follow the same pattern that does not follow the same pattern that standard INTEGER or REAL addition standard INTEGER or REAL addition does we need a new version of it.does we need a new version of it.

COMPLEX :: z, wCOMPLEX :: z, w z = (3,4)z = (3,4) w = (5,2)w = (5,2) PRINT*, z + wPRINT*, z + w

Page 18: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

COMPLEX additionCOMPLEX addition

Given two COMPLEX numbers z and w such thatz = a + bi and w = c + di

The general formula for COMPLEX number addition is:

z + w = (a + c) + (b + d)i

z = (3,4)w = (5,2)PRINT*, z + w (8,7)

Page 19: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

COMPLEX subtractionCOMPLEX subtraction

Given two COMPLEX numbers z and w such thatz = a + bi and w = c + di

The general formula for COMPLEX number addition is:

z - w = (a - c) + (b - d)i

z = (3,4)w = (5,2)PRINT*, z - w (-2,2)

Page 20: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

COMPLEX productCOMPLEX product

Given two COMPLEX numbers z and w such thatz = a + bi and w = c + di

The general formula for COMPLEX number multiplication is:

z * w = (ac - bd) + (ad - bc)i

z = (3,4)w = (5,2)PRINT*, z * w (7,-14)

Page 21: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

COMPLEX quotientCOMPLEX quotient

Given two COMPLEX numbers z and w such thatz = a + bi and w = c + di

The general formula for COMPLEX number division is:

z / w = (ac + bd)/(c2 + d2) + (bc - ad)/(c2 + d2)i

z = (3,4)w = (5,2)PRINT*, z / w (4.27,2.6)

Page 22: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Type CHARACTERType CHARACTER

We have already used CHARACTER We have already used CHARACTER data to some extent.data to some extent.

CHARACTER data is, by default, CHARACTER data is, by default, represented using ASCII character represented using ASCII character codes.codes.

ASCII stands for the American Standard ASCII stands for the American Standard Code for Information Interchange - it is Code for Information Interchange - it is the common method used by almost all the common method used by almost all computers.computers.

Page 23: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

ASCII Table (32-111)ASCII Table (32-111)32 space33 !34 “35 #36 $37 %38 &39 ‘40 (41 )42 *43 +44 ,45 -46 .47 /

48 049 150 251 352 453 554 655 756 857 958 :59 ;60 <61 =62 >63 ?

64 @65 A66 B67 C68 D69 E70 F71 G72 H73 I74 J75 K76 L77 M78 N79 O

80 P81 Q82 R83 S84 T85 U86 V87 W88 X89 Y90 Z91 [92 \93 ]94 ^95 _

96 `97 a98 b99 c100 d101 e102 f103 g104 h105 i106 j107 k108 l109 m110 n111 o

Page 24: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Character substringsCharacter substrings

A substring is a portion of a main stringA substring is a portion of a main string ExampleExample

• CHARACTER(20) :: nameCHARACTER(20) :: name• name = “George Washington”name = “George Washington”• PRINT*, name(:20)PRINT*, name(:20) George George

WashingtonWashington• PRINT*, name(:8)PRINT*, name(:8) George WGeorge W• PRINT*, name(8:)PRINT*, name(8:) WashingtonWashington• PRINT*, name(8:11)PRINT*, name(8:11) WashWash

Page 25: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

CHARACTER operatorsCHARACTER operators

Concatenation (//)Concatenation (//)• CHARACTER(6) :: fnameCHARACTER(6) :: fname• CHARACTER(9) :: lnameCHARACTER(9) :: lname• fname = “George”fname = “George”• lname = “Washington”lname = “Washington”• PRINT*, lname // “, “ // fnamePRINT*, lname // “, “ // fname

OutputOutput• Washington, GeorgeWashington, George

Page 26: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

String to string functionsString to string functions

ADJUSTL(str) - left justifies the ADJUSTL(str) - left justifies the string strstring str

ADJUSTR(str) - right justifies the ADJUSTR(str) - right justifies the string strstring str

REPEAT(str, n) - makes string REPEAT(str, n) - makes string consisting of n concatenations of consisting of n concatenations of the string strthe string str

TRIM(str) - removes trailing blanksTRIM(str) - removes trailing blanks

Page 27: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

ExamplesExamples

CHARACTER(20) :: name1, name2, CHARACTER(20) :: name1, name2, name3name3

name = “Buffalo Bill”name = “Buffalo Bill” name2 = ADJUSTR(name1)name2 = ADJUSTR(name1) name3 = ADJUSTL(name2)name3 = ADJUSTL(name2) PRINT*, REPEAT(name1(1:1), 5)PRINT*, REPEAT(name1(1:1), 5) PRINT*, TRIM(name3)PRINT*, TRIM(name3)

Buffalo Bill

Buffalo Bill

Buffalo Bill

BBBBB

Buffalo_Bill

Page 28: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Relational operatorsRelational operators

The standard relational operators The standard relational operators <, <=, >, >=, ==, /= all work with <, <=, >, >=, ==, /= all work with character variables.character variables.

IF ( name1 > name2) THEN…IF ( name1 > name2) THEN… They compare the values of the They compare the values of the

ASCII characters in corresponding ASCII characters in corresponding positions until they can determine positions until they can determine that one is less than the other.that one is less than the other.

Page 29: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Conversion FunctionsConversion Functions

ICHAR - converts a character to an ICHAR - converts a character to an integerinteger

CHAR - converts an integer to a CHAR - converts an integer to a charactercharacter

These use ASCII values by default.These use ASCII values by default.

Page 30: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

ExamplesExamples

INTEGER :: num, i, sumCHARACTER(10) :: numeralDO i=1,10 numeral(i:i) = CHAR(i+47)ENDDOPRINT*, numeral 0123456789sum = 0DO i = 1,10 sum = sum + (ICHAR(numeral(i:i))-47)ENDDOPRINT*, “sum is “, sum sum is 45

Page 31: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

File ProcessingFile Processing

Page 32: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Types of filesTypes of files

We have already dealt with reading We have already dealt with reading data from standard ASCII text files into data from standard ASCII text files into our program.our program.

This sort of file is called a ‘sequential This sort of file is called a ‘sequential access’ file. access’ file.

Sequential access means that in order Sequential access means that in order to get to a desired record you must first to get to a desired record you must first have read and processed all records have read and processed all records before it in sequence.before it in sequence.

Page 33: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Direct access filesDirect access files A direct access file is an alternative to A direct access file is an alternative to

sequential access.sequential access. In a direct access file you do not need to In a direct access file you do not need to

read the lines of the file in sequence.read the lines of the file in sequence. You can get to any record on the file by You can get to any record on the file by

accessing the storage locations directly.accessing the storage locations directly. You would want to use this for very large You would want to use this for very large

data files where reading the whole thing data files where reading the whole thing into arrays is out of the question.into arrays is out of the question.

Page 34: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

How direct access worksHow direct access works

If a file is stored on disk, then individual If a file is stored on disk, then individual records can be accessed directly if we knowrecords can be accessed directly if we know• The address of where the file startsThe address of where the file starts• How long each record is (all must be the same How long each record is (all must be the same

length)length) This means that when the file was OPENED This means that when the file was OPENED

we must know it’s record length (RECL)we must know it’s record length (RECL) When reading we must specify the record.When reading we must specify the record.

Page 35: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

ExampleExample

OPEN (12, FILE=“student.dat”, ACCESS=“DIRECT”, & RECL=48)! This file contains 100 student records. The id numbers! On the file go from 001 to 100. The records are sorted.DO PRINT*, “Please enter an id number” READ*, id IF ((id > 0) .and. (id < 100)) READ(12, ‘(1x,a15, a15, i5, a5, f4.0, f6.2)’, REC=id) & lname, fname, id, passwd, limit, usedENDDO

Page 36: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

PointersPointers

Page 37: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Pointer variablesPointer variables

A pointer is an integer that contains A pointer is an integer that contains the address of a data item in memory.the address of a data item in memory.

Smith0243632

0243632

Page 38: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Allocating pointersAllocating pointers

Pointers are created through the ALLOCATE statement.First, however, we must declare the pointer:

CHARACTER(8), POINTER :: nameptrCHARACTER(8) :: namename = “Smith”

ALLOCATE(nameptr)

nameptr => name ! NOTE: the => operator means ‘assign pointer of’

Page 39: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Pointer variablesPointer variables

Result after the last code segmentResult after the last code segment

Smith0243632

0243632

nameptr

name

Page 40: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Assignment to pointersAssignment to pointers

Pointers are created through the ALLOCATE statement.First, however, we must declare the pointer:

CHARACTER(8), POINTER :: nameptr

ALLOCATE(nameptr)

nameptr = “Smith” ! Memory has been allocated for 8 characters and! nameptr points to that memory. Then “Smith” is! Placed in that location.

Page 41: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Pointer variablesPointer variables

Result after the last code segmentResult after the last code segment

Smith0243632

0243632

nameptr

Page 42: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

ASSOCIATEDASSOCIATED

The ASSOCIATED function tells you The ASSOCIATED function tells you whether a pointer actually points whether a pointer actually points to any data.to any data.

IF (.not. ASSOCIATED(nameptr)) IF (.not. ASSOCIATED(nameptr)) THENTHEN

PRINT*, nameptr is nullPRINT*, nameptr is null ENDIFENDIF

Page 43: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Why are pointers Why are pointers important?important?

Up until now we have used only ‘static’ Up until now we have used only ‘static’ structures.structures.

A static structure is one that is defined A static structure is one that is defined and has space allocated for it - at and has space allocated for it - at compile time.compile time.

This means it’s size is fixed. Example: This means it’s size is fixed. Example: the normal arraythe normal array

Pointer however are dynamic! They allow Pointer however are dynamic! They allow us to create memory cells at runtime.us to create memory cells at runtime.

Page 44: Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647

Dynamic data structuresDynamic data structures

A dynamic structure is one that can grow A dynamic structure is one that can grow or contract as the program executes, to or contract as the program executes, to accommodate the data that is being accommodate the data that is being processed. processed.

Dynamic structures waste little memory.Dynamic structures waste little memory. They can also allow new forms of data They can also allow new forms of data

organization (rather than always using organization (rather than always using arrays)arrays)

More on this next lecture.More on this next lecture.