IEEE Floating-point overview

Here is a brief summary of the ANSI/IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Std 754-1985 and the details of how it applies to XL Fortran on specific hardware platforms. For information on the Fortran 2003 IEEE Module and arithmetic support, see the XL Fortran Language Reference.

Compiling for strict IEEE conformance

By default, XL Fortran follows most, but not all of the rules in the IEEE standard. To compile for strict compliance with the standard:

IEEE Single- and double-precision values

XL Fortran encodes single-precision and double-precision values in IEEE format. For the range and representation, see Real in the XL Fortran Language Reference.

IEEE Extended-precision values

The IEEE standard suggests, but does not mandate, a format for extended-precision values. XL Fortran does not use this format. Extended-precision values describes the format that XL Fortran uses.

Infinities and NaNs

For single-precision real values:

For double-precision real values:

These values do not correspond to any Fortran real constants. You can generate all of these by encoding the bit pattern directly, or by using the ieee_value function provided in the ieee_arithmetic module. Using the ieee_value function is the preferred programming technique, as it is allowed by the Fortran 2003 standard and the results are portable. Encoding the bit pattern directly could cause portability problems on machines using different bit patterns for the different values. All except signaling NaN values can occur as the result of arithmetic operations:

$ cat fp_values.f
real plus_inf, minus_inf, plus_nanq, minus_nanq, nans
real large

data plus_inf /z'7f800000'/
data minus_inf /z'ff800000'/
data plus_nanq /z'7fc00000'/
data minus_nanq /z'ffc00000'/
data nans /z'7f800001'/

print *, 'Special values:', plus_inf, minus_inf, plus_nanq, minus_nanq, nans

! They can also occur as the result of operations.
large = 10.0 ** 200
print *, 'Number too big for a REAL:', large * large
print *, 'Number divided by zero:', (-large) / 0.0
print *, 'Nonsensical results:', plus_inf - plus_inf, sqrt(-large)

! To find if something is a NaN, compare it to itself.
print *, 'Does a quiet NaN equal itself:', plus_nanq .eq. plus_nanq
print *, 'Does a signaling NaN equal itself:', nans .eq. nans
! Only for a NaN is this comparison false.

end
$ xlf95 -o fp_values fp_values.f
** _main   === End of Compilation 1 ===
1501-510  Compilation successful for file fp_values.f.
$ fp_values
 Special values: INF -INF NAN -NAN NAN
 Number too big for a REAL: INF
 Number divided by zero: -INF
 Nonsensical results: NAN NAN
 Does a quiet NaN equal itself: F
 Does a signaling NaN equal itself: F

Exception-handling model

The IEEE standard defines several exception conditions that can occur:

OVERFLOW
The exponent of a value is too large to be represented.
UNDERFLOW
A nonzero value is so small that it cannot be represented without an extraordinary loss of accuracy. The value can be represented only as zero or a denormal number.
ZERODIVIDE
A finite nonzero value is divided by zero.
INVALID
Operations are performed on values for which the results are not defined. These include:
INEXACT
A computed value cannot be represented exactly, so a rounding error is introduced. (This exception is very common.)

XL Fortran always detects these exceptions when they occur, but by default does not take any special action. Calculation continues, usually with a NaN or infinity value as the result. If you want to be automatically informed when an exception occurs, you can turn on exception trapping through compiler options or calls to intrinsic subprograms. However, different results, intended to be manipulated by exception handlers, are produced:

Table 24. Results of IEEE exceptions, with and without trapping enabled
  Overflow Underflow Zerodivide Invalid Inexact
Exceptions not enabled (default) INF Denormalized number INF NaN Rounded result
Exceptions enabled Unnormalized number with biased exponent Unnormalized number with biased exponent No result No result Rounded result
Note:
Because different results are possible, it is very important to make sure that any exceptions that are generated are handled correctly. See Detecting and trapping floating-point exceptions for instructions on doing so.