SuivantBasNiv. sup.

5.1 Data Types 

All the information processed by a digital computer is held internally in the form of binary digits or bits. Suitable collections of bits can be used to represent many different types of data including numbers and strings of characters. It is not necessary to know how the information is represented internally in order to write Fortran programs, only that there is a different representation for each type of data. The data type of each item also determines what operations can be carried out on it: thus arithmetic operations can be carried out on numbers, whereas character strings can be split up or joined together. The data type of each item is fixed when the program is written.

Fortran, with its emphasis on numerical operations, has four data types just for numbers. These are collectively known as the arithmetic data types. Arithmetic expressions can include mixtures of data types and, in most cases, automatic type conversions are provided. In other circumstances, however, especially in procedure calls, there is no provision for automatic type conversion and it is essential for data types to match exactly.

The range and precision of the arithmetic data types are not specified by the Standard: typical values are indicated below, but the only way to be sure is to check the manuals provided with your own Fortran system.

Several intrinsic functions are available to convert from one data type to another. Conversion from character strings to numbers and vice-versa can be complicated; these are best carried out with the internal file READ and WRITE statements (see section 10.3).

There are, as yet, no user-defined or structured data types in Fortran.

Standard Data Types 

The table below summarises the properties of the six data types provided in Standard Fortran:
Data type Characteristics
Integer Whole numbers stored exactly.
Real Numbers, which may have fractional parts, stored using a floating-point representation with limited precision.
Double Precision Similar to real but with greater precision.
Complex Complex numbers: stored as an ordered pair of real numbers.
Logical A Boolean value, i.e. one which is either true or false.
Character A string of characters of fixed length.
The first four types (integer, real, double precision, and complex) all hold numerical information and are collectively known as arithmetic data types.

Integer Type 

The integer data type can only represent whole numbers but they are stored exactly in all circumstances. Integers are often used to count discrete objects such as elements of an array, characters in a string, or iterations of a loop.

The range of numbers covered by the integer type is system-dependent. The majority of computers use 32 bits for their integer arithmetic (1 bit for the sign and 31 for the magnitude) giving a number range of -2, 147, 483, 648 to +2, 147, 483, 647. Some systems have an even larger integer range but a few very small systems only allow 16-bit integer arithmetic so that their integer range is only -32, 768 to +32, 767.

Real Type 

Most scientific applications use the real data type more than anything else. Real values are stored internally using a floating-point representation which gives a larger range than the integer type but the values are not, in general, stored exactly. Both the range and precision are machine dependent.

In practice most machines use at least 32 bits to store real numbers. Many systems now use the IEEE Standard representation: for 32-bit numbers this gives a precision of just over 7 decimal digits and allows a number range from around 10-38 to just over 10+38. This can be something of a limitation because there are many types of calculation, especially in physics and astronomy, which lead to numbers in excess of 1040. Some computers designed expressly for scientific work, sometimes called "super-computers", allocate 64 bits for real numbers so that the numerical precision is much larger; the range is often larger as well. On such machines it is rarely necessary to use the double precision type.

Double Precision Type 

Double precision is an alternative floating-point type. The Fortran Standard only specifies that it should have greater precision than the real type but in practice, since the double precision storage unit is twice the size, it is safe to assume that the precision is at least doubled. The number range may, however, be the same as that for real type.

Although double precision values occupy twice as much memory as real (or integer) values, computations on them do not necessarily take twice as long.

Complex Type 

The complex data type stores two real values as a single entity. There is no double precision complex type in Standard Fortran.

Complex numbers arise naturally when extracting the roots of negative numbers and are used in many branches of mathematics, physics, and engineering. A complex number is often represented as (A + iB), where A and B are the real and imaginary parts respectively and i2 = -1. Electrical engineers, having used the letter i to represent current, use the notation (A + jB) instead.

Although the rules for manipulating complex numbers are straight-forward, it is convenient to have the Fortran system to do the work. It is usually more efficient as well, because the computer can use its internal registers to store the intermediate products in complex arithmetic. Exponentiation and the four regular arithmetic operators can be used on complex values, and various intrinsic functions are also provided such as square-root, logarithms, and the trigonometric functions.

Logical Type 

The logical data type is mainly used in conjunction with IF statements which select a course of action according to whether some condition is true or false. A logical variable (or array element) may be used to store such a condition value for future use. Logical variables and arrays are also useful when dealing with two-valued data such as whether a person is male or female, a file open or closed, power on or off, etc.

Some programmers seem reluctant to use logical variables and arrays because they feel that it must be inefficient to use an entire computer word of perhaps 32 bits to store just one bit of information. In fact the extra code needed to implement a more efficient data packing scheme usually wastes more memory than the logical variables would have occupied.

Character Type 

The character type is unique in that each character item has a length defined for it: this is the number of characters that it holds. In general the length is fixed when the item is declared and cannot be altered during execution. The only exception to this is for dummy arguments of procedures: here it is possible for the dummy argument to acquire the length of the corresponding actual argument. Using this facility, general-purpose procedures can be written to operate on character strings irrespective of their length. In addition, the rules for character assignment take care of mismatched lengths by truncating or padding with blanks on the right as necessary. This means that the Fortran character type has many of the properties of a genuine variable-length character-handling system.

The maximum length of a character item is system-dependent: it is safe to assume that all systems will allow strings of up to 255 characters, a length limit of 32767 (or even more) is quite common. The minimum length of a character item is one character; empty or null strings are not permitted.

Storage Units 

Although the Fortran Standard does not specify the absolute amount of memory to be allocated to each data type, it does specify the relative amounts. This is not important very often, only when constructing unformatted direct-access records or when using COMMON and EQUIVALENCE statements. The rules are as follows:
Data types Storage units
integer, real, logical 1 numerical storage unit
complex, double precision2 numerical storage units
character*(N) N character storage units
In the case of an array the number of storage units must be multiplied by the total number of elements in the array.

The relationship between the numeric and character storage units is deliberately undefined because it is entirely system-dependent.

Guidelines 

It is usually fairly clear which data type to choose for each item in a program, though there are some borderline cases among the various arithmetic data types.

When processing data which are inherently integers, such as the number of seeds which germinate in each plot, or the number of photons detected in each time interval, it is not always clear whether to use integer or real arrays to store them. They both use the same memory space but on some machines additions and subtractions are faster on integers than on floating-point numbers. In practice, however, any savings can be swallowed up in the data type conversions that are usually necessary in subsequent processing. The main snag with integers is the limited range; on some machines integer overflow is not detected whereas floating-point overflows nearly always produce error messages.

If your machine stores its real variables in 32-bit words then the precision of around 1 in 107 is likely to be inadequate in some applications. This imprecision is equivalent to an error of several pence in a million pounds, or around ten milliseconds in a day. If errors of this order are significant you should consider using the double precision type instead. This will normally reduce the errors by at least another factor of 107. Mixing data types increases the risks of making mistakes and it is often simpler and safer to use the double precision type instead of real throughout the program, even though this may use slightly more memory and processor time.

Although automatic type conversions are provided for the arithmetic types in expressions, in other cases such as procedure calls it is essential for each actual argument to have the same data type as the corresponding dummy argument. Since program units are compiled independently, it is difficult for either the compiler or the linker to detect type mismatches in calls to external procedures.

Non-standard Data Types 

Although Standard Fortran only provides the above six data types, many systems provide additional ones. You may come across data type names such as: LOGICAL*1, INTEGER*2, REAL*8, COMPLEX*16, etc. The number after the asterisk indicates the number of bytes of storage used for each datum (a byte being a group of 8 bits). This notation has a certain logic but is totally non-standard. The use of a term like REAL*8 when it is simply a synonym for DOUBLE PRECISION seems particularly pointless. There are, of course, circumstances when types such as COMPLEX*16 are necessary but the price to be paid is the loss of portability.

SuivantHautNiv. sup.