Compaq Fortran
User Manual for
Tru64 UNIX and Linux Alpha Systems


Previous Contents Index

Compaq Fortran supports IEEE exception handling, allowing you to test for infinity by using a comparison of floating-point data (such as generating positive infinity by using a calculation like x=1.0/0 and comparing x to the calculated number).

The appropriate f90 command -fpen options or calling the for_set_fpe routine with appropriate arguments allows program continuation when a calculation results in a divide by zero, overflow, or invalid data arithmetic exception, generating an exceptional value (a NaN or Infinity (+ or --)).

To test for a NaN when Compaq Fortran allows continuation for arithmetic exceptions, you can use the ISNAN intrinsic function.

For example, you might use the following code to test a DOUBLE PRECISION (REAL (KIND=8)) value:


      DOUBLE PRECISION A, B, F 
      A = 0. 
      B = 0. 
 
!     Perform calculations with variables A and B 
      . 
      . 
      . 
 
!     f contains the value to check against a particular NaN 
 
      F = A / B 
 
      IF (ISNAN(F)) THEN 
         WRITE (6,*) '--> Variable F contains a NaN value <--' 
      ENDIF 
 
!     Inform user that f has the hardware quiet NaN value 
 
!     Perform calculations with variable F (or stop program early) 
 
      END PROGRAM 

This program might be compiled with -fpe2 or -fpe4 to allow:

The FP_CLASS intrinsic function is also available to check for exceptional values (see the Compaq Fortran Language Reference Manual and the file /usr/include/fordef.f ).

For More Information:

9.5 Character Representation

A character string is a contiguous sequence of bytes in memory, as shown in Figure 9-12.

Figure 9-12 CHARACTER Data Representation


A character string is specified by two attributes: the address A of the first byte of the string, and the length L of the string in bytes. The length L of a string is in the range 1 through 65,535.

For More Information:

9.6 Hollerith Representation

Hollerith constants are stored internally, one character per byte. When Hollerith constants contain the ASCII representation of characters, they resemble the storage of character data (see Figure 9-12).

When Hollerith constants store numeric data, they usually have a length of one, two, four, or eight bytes and resemble the corresponding numeric data type.

For More Information:


Chapter 10
Converting Unformatted Numeric Data

This chapter describes how you can use Compaq Fortran to read and write unformatted numeric data in certain nonnative formats, including big endian IEEE and VAX floating-point formats.

On Compaq Tru64 UNIX and Linux systems, Compaq Fortran supports the following little endian floating-point formats in memory:
Floating-Point Size Format in Memory
KIND=4 IEEE S_float
KIND=8 IEEE T_float
KIND=16 Compaq IEEE style X_float

If your program needs to read or write unformatted data files containing a floating-point format that differs from the format in memory for that data size, you can request that the unformatted data be converted.

Converting unformatted data is generally faster than converting formatted data and is less likely to lose precision for floating-point numbers.

10.1 Endian Order of Numeric Formats

Data storage in different computers use a convention of either little endian or big endian storage. The storage convention generally applies to numeric values that span multiple bytes, as follows:

Figure 10-1 shows the difference between the two byte-ordering schemes.

Figure 10-1 Little and Big Endian Storage of an INTEGER Value


Moving data files between big endian and little endian computers requires that the data be converted.

10.2 Native and Supported Nonnative Numeric Formats

Compaq Fortran provides the capability for programs to read and write unformatted data (originally written using unformatted I/O statements) in several nonnative floating-point formats and in big endian INTEGER or floating-point format.

When reading a nonnative unformatted format, the nonnative format on disk must be converted to native format in memory. Similarly, native data in memory can be written to a nonnative unformatted format. If a converted nonnative value is outside the range of the native data type, a run-time message appears (listed in Table 8-2).

Supported native and nonnative floating-point formats include:

The native memory format uses little endian integers and little endian IEEE floating-point formats, as follows:

Table 10-1 lists the keywords for the supported unformatted file data formats. Use the appropriate keyword after the -convert option (such as -convert cray ) or as an environment variable value (see Section 10.4.1 and Section 10.4.2).

Table 10-1 Unformatted Numeric Formats, Keywords, and Supported Data Types
Recognized Keyword1 Description
BIG_ENDIAN Big endian integer data of the appropriate INTEGER size (one, two, or four bytes) and big endian IEEE floating-point formats for REAL and COMPLEX single- and double-precision numbers. INTEGER (KIND=1) or INTEGER*1 data is the same for little endian and big endian.
CRAY Big endian integer data of the appropriate INTEGER size (one, two, four, or eight bytes) and big endian CRAY proprietary floating-point format for REAL and COMPLEX single- and double-precision numbers.
FDX Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian Compaq proprietary floating-point formats:
VAX F_float for REAL (KIND=4) and COMPLEX (KIND=4)
VAX D_float for REAL (KIND=8) and COMPLEX (KIND=8)
IEEE style X_float for REAL (KIND=16) and COMPLEX (KIND=16)
FGX Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian Compaq proprietary floating-point formats:
VAX F_float for REAL (KIND=4) and COMPLEX (KIND=4)
VAX G_float for REAL (KIND=8) and COMPLEX (KIND=8)
IEEE style X_float for REAL (KIND=16) and COMPLEX (KIND=16)
IBM Big endian integer data of the appropriate INTEGER size (one, two, or four bytes) and big endian IBM proprietary (System\370 and similar) floating-point format for REAL and COMPLEX single- and double-precision numbers.
LITTLE_ENDIAN Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following native little endian IEEE floating-point formats:
S_float for REAL (KIND=4) and COMPLEX (KIND=4)
T_float for REAL (KIND=8) and COMPLEX (KIND=8)
IEEE style X_float for REAL (KIND=16) and COMPLEX (KIND=16)
NATIVE No conversion occurs between memory and disk. This is the default for unformatted files.
VAXD Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian VAX Compaq proprietary floating-point formats:
VAX F_float for REAL (KIND=4) and COMPLEX (KIND=4)
VAX D_float for REAL (KIND=8) and COMPLEX (KIND=8)
VAXG Native little endian integers of the appropriate INTEGER size (one, two, four, or eight bytes) and the following little endian VAX Compaq proprietary floating-point formats:
VAX F_float for REAL (KIND=4) and COMPLEX (KIND=4)
VAX G_float for REAL (KIND=8) and COMPLEX (KIND=8)


1When using the data type as a -convert keyword option on the f90 command line, the data type keyword must be in lowercase, such as -convert big_endian.

While this solution is not expected to fulfill all floating-point conversion needs, it provides the capability to read and write various types of unformatted nonnative floating-point data.

For More Information:

Note

1 IEEE floating-point formats are defined in the IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985, Institute of Electrical and Electronics Engineers, August 1985.

10.3 Limitations of Numeric Conversion

The Compaq Fortran floating-point conversion solution is not expected to fulfill all floating-point conversion needs.

Data (variables) contained in derived types and record structures (specified in a STRUCTURE statement) are not converted. When the variables are later examined as separate fields by the program, they will remain in the binary format they were stored in on disk, unless the program is modified.

If a program reads an I/O record containing multiple floating-point fields into an integer array (instead of their respective variables), the fields will not be converted. When the fields are later examined as separate fields by the program, they will remain in the binary format they were stored in on disk, unless the program is modified.

With EQUIVALENCE statements, the data type of the variable named in the I/O statement is used.

10.4 Methods of Specifying the Unformatted Numeric Format

The five methods you can use to specify the type of nonnative (or native) format are as follows:

If you specify more than one method, the order of precedence when you open a file with unformatted data is:

  1. Check for a FORT_CONVERTn environment variable
  2. Check for a FORT_CONVERT.ext environment variable
  3. Check the OPEN statement CONVERT specifier
  4. Check whether an OPTIONS statement with a /CONVERT=(keyword) qualifier was present when the program was compiled
  5. Check whether the f90 -convert keyword option was used when the program was compiled

If none of these methods are specified, no conversion occurs between disk and memory. Data should therefore be in the native memory format (little endian integer and little endian IEEE format) or otherwise translated by the application program.

Any keyword listed in Table 10-1 can be used with any of these methods.

If you are uncertain about the format, you can do the following:

10.4.1 Environment Variable FORT_CONVERTn Method

You can use the environment variable method to specify multiple formats in a single program, usually one format for each unit number. You specify the numeric format at run time by setting the appropriate environment variable before you open that unit number. For example, to specify the numeric format for unit 9, set environment variable FORT_CONVERT9 to the appropriate value (such as BIG_ENDIAN) before you run the program.

When you open the file, the environment variable is always used, since this method takes precedence over the f90 command option methods. For instance, you might use this method to specify that different unformatted numeric formats for different unit numbers (perhaps in a script file that sets the environment variable before running the program).

For example, assume you have a previously compiled program that reads numeric data from unit 28 and writes it to unit 29 using unformatted I/O statements. You want the program to read nonnative big endian (IEEE floating-point) format from unit 28 and write that data in native little endian format to unit 29.

In this case, the data is converted from big endian IEEE format to native little endian IEEE memory format (S_float, T_float, X_float) when read from unit 28, and then written without conversion in native little endian IEEE format to unit 29.

Without requiring source code modification or recompilation of this program, the following C shell command sequence sets the appropriate environment variables before running the program ( /usr/userc/conv_ieee.out ):


% setenv FORT_CONVERT28 BIG_ENDIAN
% setenv FORT_CONVERT29 NATIVE
% /usr/userc/conv_ieee.out

Figure 10-2 shows the data formats used on disk and in memory when the example file /usr/userc/conv_ieee.out is run after the environment variables are set with shell commands.

Figure 10-2 Sample Unformatted File Conversion


For more information on the shell commands you can use to set or unset environment variables, see Appendix B.

10.4.2 Environment Variable FORT_CONVERT.ext Method

You can use this method to specify formats in a single program, usually one format for each specified file name extension (suffix). You specify the numeric format at run time by setting the appropriate environment variable before an implicit or explicit OPEN statement to one or more unformatted files.

For example, assume you have a previously compiled program that reads floating-point numeric data from one file and writes to another file using unformatted I/O statements. You want the program to read nonnative big endian (IEEE floating-point) format from a file with a .dat file extension suffix and write that data in native little endian format to a file with a suffix of .data. In this case, the data is converted from big endian IEEE format to native little endian IEEE memory format when read from file.dat, and then written without conversion in native little endian IEEE format to the file with a suffix of .data, assuming that environment variables FORT_CONVERT.DATA and FORT_CONVERTn (for that unit number) are not defined.

Without requiring source code modification or recompilation of this program, the following command sequence sets the appropriate environment variables before running the program ( /usr/userc/proj2/cvbigend.exe ):


% setenv FORT_CONVERT.DAT BIG_ENDIAN
% /usr/userc/proj2/cvbigend.exe

The FORT_CONVERTn method takes precedence over this method. When the appropriate environment variable is set when you open the file, the FORT_CONVERT.ext environment variable is used if a FORT_CONVERTn environment variable is not set for the unit number.

The FORT_CONVERTn and FORT_CONVERT.ext environment variable methods take precedence over the other methods. For instance, you might use this method to specify that a unit number will use a particular format instead of the format specified in the program (perhaps for a one-time file conversion).

The file name extension (suffix) is case-sensitive. The extension must be part of the file name (not the directory path).

10.4.3 OPEN Statement CONVERT='keyword' Method

You can use the OPEN statement method to specify multiple formats in a single program, usually one format for each specified unit number. This method requires an explicit file OPEN statement to specify the numeric format of the file for that unit number.

This method takes precedence over the OPTIONS statement or the -convert keyword method, but has a lower precedence than the environment variable methods.

The following source code shows an OPEN statement coded for unformatted VAXD numeric data (read from unit 15), and an OPEN statement coded for unformatted native little endian format (written to unit 20). The absence of the CONVERT specifier (in the second OPEN statement) or environment variable FORT_CONVERT20 indicates native little endian data for unit 20:


  OPEN (CONVERT='VAXD', FILE='graph3.dat', FORM='UNFORMATTED', UNIT=15) 
  . 
  . 
  . 
  OPEN (FILE='graph3_ieee.dat', FORM='UNFORMATTED', UNIT=20) 

A hard-coded OPEN statement CONVERT specifier keyword value cannot be changed after compile time. However, to allow selection of a particular format at run time, you can equate the CONVERT specifier to a variable and provide the user with a menu that allows selection of the appropriate format (menu choice sets the variable) before the OPEN occurs.

You can also select a particular format for a unit number at run time by using the environment variable method (see Section 10.4.1), which takes precedence over the OPEN statement CONVERT specifier method.

You can issue an INQUIRE statement (by unit number) to an opened file to obtain the current CONVERT option in use.

10.4.4 OPTIONS Statement /CONVERT=keyword Method

You can only specify one numeric file format for all unit numbers using this method, unless you also use one of the FORT_CONVERT environment variable methods or the OPEN statement CONVERT specifier method.

You specify the numeric format at compile time and must compile all routines under the same OPTIONS statement CONVERT=keyword qualifier. You could use one source program and compile it using different f90 commands to create multiple executable programs that each read a certain format.

The environment variables and OPEN CONVERT specifier methods take precedence over this method. For instance, you might use the environment variables or OPEN CONVERT specifier method to specify each unit number that will use a format other than that specified using the f90 command option method. This method takes precedence over the f90 command -convert keyword option method.

You can use OPTIONS statements to specify the appropriate floating-point formats (in memory and in unformatted files) instead of using the corresponding f90 command options. For example, to use VAX G_float (along with VAX F_float and VAX H_float) as the unformatted file format, specify the following OPTIONS statement:


  OPTIONS /CONVERT=VAXG 

Because this method affects all unit numbers, you cannot read data in one format and write it in another format using the OPTIONS statement method, unless you use it in combination with one of the environment variable methods or the OPEN statement CONVERT keyword method to specify a different format for a particular unit number.

For More Information:

On the OPTIONS statement, see the Compaq Fortran Language Reference Manual.


Previous Next Contents Index