



Format Data Descriptors A, E, F, G, I, L
|
w, m, d, and e used with these data descriptors represent unsigned
integer constants; w and e must be greater than zero.
|
3F6.0 is equivalent to F6.0,F6.0,F6.0
This facility is particularly useful when handling arrays.
General Rules for Numeric Input/Output
Numbers are always converted using the decimal number base: there is no provision in Standard Fortran for transfers in other number bases such as octal or hexadecimal. More complicated conversions such as these can be performed with the aid of internal files.
On output number are generally right-justified in the specified field; leading
blanks are supplied where necessary. Negative values are always preceded by a minus
sign (for which space must be allowed in the field); zero is always unsigned; the SP
and SS descriptors control whether positive numbers are to be preceded by a plus
sign or not. A number which is too large to fit into its field will appear instead as a
set of w asterisks.
On input numbers should be right-justified in each field. All forms of constant
permitted in a Fortran program can safely be used in an input field of the
corresponding type, as long there are no embedded or trailing blanks. Leading blanks
are always ignored; a field which is entirely blank will be read as zero. The
treatment of embedded and trailing blanks can be controlled with the BN
and BZ descriptors. The rules here are another relic of very early Fortran
systems.
When reading a file which has been connected by means of an OPEN statement
(provided it does not contain BLANK='ZERO') all embedded and trailing blanks in
numeric input fields are treated as nulls, i.e. they are ignored. In all other cases, such
as input from the standard pre-connected file or from an internal file, embedded and
trailing blanks are treated as zeros. These defaults can be altered with the BN and BZ
control descriptors. It is hard to imagine any circumstances in which it is
desirable to interpret embedded blanks as zeros; the default settings are
particularly ill-chosen since numbers entered by a user at a terminal are often
left-justified and may appear to be padded out with zeros. Errors from this
source can be avoided by using BN at the beginning of all input format
specifications.
An integer value written with Iw appears right-justified in a field of w characters with leading blanks. Iw.m is similar but ensures that at least m digits will appear even if leading zeros are necessary. This is useful, for instance, to output the times in hours and minutes:
NHOURS = 8 MINUTE = 6 WRITE(UNIT=*, FMT='(I4.2, I2.2)') NHOURS, MINUTE |
|
Iw and Iw.m are identical. Note that an integer field must not
contain a decimal point, exponent, or any punctuation characters such as
commas.
Floating Point Data (Ew.d, Ew.dEe, Fw.d, Gw.d, Gw.dEe)
Data of any of the floating-point types (Real, Double Precision, and Complex)
may be transferred using any of the descriptors E, F, or G. For each complex
number two descriptors must be provided, one for each component; these
components may be separated, if required, by control descriptors. On output
numbers are rounded to the specified number of digits. All floating-point data
transfers are affected by the setting of the scale-factor; this is initially zero
but can be altered by the P control descriptor, as explained in the section
10.9.
Output using Fw.d produces a fixed-point value in a field of w characters with
exactly d digits after the decimal point. The decimal point is present even if w is zero,
so that if a sign is produced there is only space for, at most, w-2 digits before the
decimal point. If it is really important to suppress the decimal point in numbers with
no fractional part one way is to use a format specification of the form (F15.0,TL1)...
so that the next field starts one place to the left and over-writes the decimal point.
Another way is to copy the number to an integer variable and write it with
an I descriptor, but note the limited range of integers on most systems. F
format is especially convenient in tabular layouts since the decimal points will
line up in successive records, but it is not suitable for very large or small
numbers.
Output with Ew.d produces a number in exponential or "scientific" notation. The
mantissa will be between 0.1 and 1 (if the scale-factor is zero). The form Ew.dEe
specifies that there should be exactly e digits in the exponent. This form must be
used if the exponent will have more than three digits (although this problem does not
arise on machines on which the number range is too small). E format can be used to
handle numbers of any magnitude. The disadvantage is that exceptionally
large or small values do not stand out very well in the resulting columns of
figures.
Gw.d is the general-purpose descriptor: if the value is greater than 0.1 but not too
large to fit it the field it will be written using a fixed-point format with
d digits in total and with 4 blanks at the end of the field; otherwise it is
equivalent to Ew.d format. The form Gw.dEe allows you to specify the length of
the exponent; if a fixed-point format is chosen there are e+2 blanks at the
end.
The next example shows the different properties of these three formats on output:
X = 123.456789
Y = 0.09876543
WRITE(UNIT=*, FMT='(E12.5, F12.5, G12.5)') X,X,X, Y,Y,Y
|
representing the blank):
|
E, F, and G descriptors have identical effects: if the input field
contains an explicit decimal point it always takes precedence, otherwise the last d
digits are taken as the decimal fraction. If an exponent is used it may be preceded by
E or D (but the exponent letter is optional if the exponent is signed). If the input field
provides more digits than the internal storage can utilise, the extra precision is
ignored. It is usually best to use (Fw.0) which will cope with all common
floating-point or even integer forms.
When a logical value is written with Lw the field will contain the letter T or F
preceded by (w-1) blanks. On input the field must contain the letter T or F;
the letter may be preceded by a decimal point and any number of blanks.
Characters after the T or F are ignored. Thus the forms .TRUE. and .FALSE. are
acceptable.
If the A descriptor is used without an explicit field-width then the length of the character item in the data-transfer list determines it. This is generally what is required but note that the position of the remaining items in the record will change if the length of the character item is altered.
If is important to use fixed column layouts the form Aw may be preferred: it
always uses a field w characters wide. On output if the actual length len is less than
w the value is right-justified in the field and preceded by (w-len) blanks; otherwise
only the first w characters are output, the rest are ignored. On input if the length
len is less than w then the right-most len characters are used, otherwise
w characters will be read into the character variable with (len-w) blanks
appended.