• Aucun résultat trouvé

Data Type Selection

Dans le document The Fortran 2003 Handbook (Page 69-73)

4 Data Types

4.1 Data Type Selection

The appropriate intrinsic type for a data entity is often obvious. More careful thought is likely to be required for the selection of suitable kind parameter values, derived types and classes.

Table 4-1 Intrinsic types for computational tasks

Task Type of data

Calculating typical numeric results Real data Calculating in the complex domain Complex data

Counting Integer data

Making decisions Logical data

Explaining Character data

Figure 4-1 Fortran data types Table 4-2 Intrinsically-defined operations Type of data Intrinsic operations

Real, complex, integer Addition, subtraction, multiplication, division, exponentiation, negation, comparison, identity

Logical Negation, conjunction, disjunction, equivalence and nonequivalence Character Concatenation, comparison

Derived None

Fortran data types

Derived types Intrinsic types

Numeric types Nonnumeric types

Real Complex

Integer Logical Character

Data Types 63

4.1.1 Kinds of Intrinsic Types

Once the type is decided, it may be necessary for the programmer to consider which

“kind” of the type to use. “Kind” is a technical term in Fortran. Each of the intrinsic types has a kind parameter that selects a processor-dependent representation of ob-jects of that type and kind. Each compiler supports a particular set of representations from which the programmer can select. If no kind parameter value is specified, the de-fault kind is assumed.

Kinds are known to the processor as integer values, but if a program is to be porta-ble, the actual numbers should not be used because the particular kind values for the intrinsic types are processor dependent. For portability, appropriate kind values should be determined using the procedures described in 4.3, 13.3.1.2, and 14.3.3.7 and assigned to named constants, which are then used in type specifiers, literal constants, and kind arguments of intrinsic functions. When named constants are used to desig-nate kinds, only the value of the constant matters—not the names of the constant. If two different named constants have the same numerical value and are used as kind pa-rameters, then they will represent the same kind.

The intrinsics for determining kind values all return negative values when appro-priate kinds are not available for the particular compiler. Negative values are guaran-teed to cause compilation error diagnostics when used as kind values, thus ensuring that programs will not accidentally be run with kinds that do not meet specified re-quirements.

The Fortran kind parameters for each of the intrinsic types serve the following pur-poses:

1. Real. The real kind parameter primarily selects the precision and range of the rep-resentation. There may also be multiple representations for the same precision and range, for example native and IEEE representations. The standard requires that each compiler support at least two real kinds which must have different precisions.

The default real kind is the lower precision of these. Compilers may support addi-tional real kinds to provide other precisions or other representations with the same precisions.

Fortran 77 did not have kind parameters but did provide two kinds for the real type: REAL and DOUBLE PRECISION. REAL is often referred to as single preci-sion. It treated double precision real as a separate type. Fortran 90 and later ver-sions, while remaining compatible, treat double precision as a separate kind of real. That is, there are two ways to specify double precision real: one is with a REAL specifier with the kind corresponding to double precision, and the other is with a DOUBLE PRECISION specifier.

Programs with default REAL and DOUBLE PRECISION declarations are not nu-merically portable across machine architectures with different word sizes. Each compiler vendor chooses a representation for the real type that is efficient on the host machine. For example, a representation that will fit into 32 bits might be cho-sen on a 32-bit-word machine while a reprecho-sentation that fits into 64 bits might be chosen for a 64-bit-word machine. If a 64-bit representation is required for the

nu-64 Chapter 4 merical stability of the algorithm, DOUBLE PRECISION declarations must be used on the 32-bit machine. When the program is moved to the 64-bit machine, the DOUBLE PRECISION declarations usually would be changed to REAL declara-tions because a 128-bit representation is not needed and probably would degrade the performance of the program. A programmer can use kind parameters in REAL declarations to specify a required minimum precision of perhaps 12 decimal digits.

When the program is run on the 32-bit machine, it will use the kind corresponding to double precision. When the same program (without change) is run on the 64-bit machine, the kind corresponding to single precision will be used.

2. Complex. The kind parameter for complex selects the same representation as that for real. Every real kind has a corresponding complex kind and vice versa.

3. Integer. The integer kind parameter primarily selects the range of integer values that can be represented. Some representations might provide for a large range at the cost of large storage size, while others might provide for small storage size at the cost of correspondingly smaller range. In principle it is also possible for there to be multiple representations for the same range, perhaps differing in byte order or in the representation of negative values. Only one integer kind is required in a standard-conforming processor, but more are permitted. The default integer kind is represented in the same size as the default real kind.

4. Character. The character kind parameter selects a character set. The default charac-ter type usually has an underlying machine representation of a single byte (8 bits).

This is adequate to represent or 256 different characters, which is more than enough for alphabetic languages. However, ideographic languages, such as Japa-nese and ChiJapa-nese, have several thousand graphic symbols that require at least a two-byte representation (16 bits). To accommodate this spectrum of users, Fortran makes provision for (although it does not require implementation of) different kinds of character data. Because these additional kinds of character data are not re-quired for standard-conforming Fortran processors, many processors intended for English-speaking Fortran users might not support ideographic languages. Never-theless, the character kind mechanism allows an implementation to support an al-phabetic language or an ideographic language or both simultaneously. The standard makes particular provisions for the ASCII and ISO 10646 character sets, but support for them is optional.

5. Logical. Because the logical type has only two values (true and false), it could be represented in a single bit. Although efficient in terms of storage, single-bit repre-sentations can be inefficient in computation time. For storage association, the de-fault logical type is represented in the same size as the dede-fault real type, but alternative representations of logical data are permitted; that is, a nondefault logi-cal kind might be represented in a byte or in a bit for architectural efficiency or application requirements.

28

Data Types 65

4.1.2 Derived Types

Sometimes it is easier to think about an essential element of a problem as several pieces of related data. Arrays can be used to collect homogeneous data (all of the same type) into a single variable. In contrast, a structure is a collection of possibly nonhomoge-neous data in a single variable. To declare a structure, it is first necessary to define a type that has components of the desired types. The structure is then declared as an ob-ject of this user-defined (or derived) type. In the example below, first a type named PA-TIENT is defined, then two structures JOHN_JONES and SALLY_SMITH are declared.

TYPE PATIENT

INTEGER PULSE_RATE REAL TEMPERATURE CHARACTER (LEN=300) PROGNOSIS END TYPE PATIENT

TYPE (PATIENT) JOHN_JONES, SALLY_SMITH

Type PATIENT has three components, each of a different intrinsic type (integer, real, and character). In practice, a type of this nature probably would have even more com-ponents, such as the patient’s name and address, insurance company, room number in the hospital, etc. For purposes of illustration, three components are sufficient.

JOHN_JONES and SALLY_SMITH are variables (or structures) of type PATIENT. A type definition indicates names, types, and attributes for its components; it does not declare any variables that have these components. Just as with the intrinsic types, a type declaration is needed to declare variables of this type. There can be any number of variables of type PATIENT; there can be subprogram arguments and function results of type PATIENT; there can be arrays of type PATIENT; and operations, such as .appen-dectomy., can be defined that manipulate objects of type PATIENT. Thus the derived-type definition can be used as a way to specify a pattern for a particular collection of related but nonhomogeneous data; but, because the user can define the pattern, a num-ber of other capabilities are available.

4.1.3 Classes

Sometimes there are multiple derived types that share some components and opera-tions, but have other components and operations that differ. It is desirable to take ad-vantage of the commonalities in order to minimize code duplication and maintenance problems. In that case, a class of related types can be defined in a tree structure. The shared components and operations are defined for a base type. The other types are de-fined as extension types of the base type.

For example, consider a program that has multiple linked lists, each with a differ-ent type of object. Each of these linked lists has a corresponding node type. The basic linked list operations and the node components needed to support those operations are the same for all the linked lists. The object type and any operations related to it differ among the lists. In this case, a base type can be defined for a linked list node with no object. The types for the nodes are defined as extensions of the base type, each of which adds different components to the base type.

66 Chapter 4

Dans le document The Fortran 2003 Handbook (Page 69-73)