Sun Studio 12: Fortran Programming Guide

6.2 IEEE Floating-Point Arithmetic

IEEE arithmetic is a relatively new way of dealing with arithmetic operations that result in such problems as invalid operand, division by zero, overflow, underflow, or inexact result. The differences are in rounding, handling numbers near zero, and handling numbers near the machine maximum.

The IEEE standard supports user handling of exceptions, rounding, and precision. Consequently, the standard supports interval arithmetic and diagnosis of anomalies. IEEE Standard 754 makes it possible to standardize elementary functions like exp and cos, to create high precision arithmetic, and to couple numerical and symbolic algebraic computation.

IEEE arithmetic offers users greater control over computation than does any other kind of floating-point arithmetic. The standard simplifies the task of writing numerically sophisticated, portable programs. Many questions about floating-point arithmetic concern elementary operations on numbers. For example:

Another class of questions concerns floating-point exceptions and exception handling. What happens if you:

In older arithmetic models, the first class of questions might not have the expected answers, while the exceptional cases in the second class might all have the same result: the program aborts on the spot or proceeds with garbage results.

The standard ensures that operations yield the mathematically expected results with the expected properties. It also ensures that exceptional cases yield specified results, unless the user specifically makes other choices.

For example, the exceptional values +Inf, -Inf, and NaN are introduced intuitively:

big*big = +Inf Positive infinity

big*(-big) = -Inf Negative infinity

num/0.0 = +Inf Where num > 0.0

num/0.0 = -Inf Where num < 0.0

0.0/0.0 = NaN Not a Number

Also, five types of floating-point exception are identified:

The implementation of the IEEE standard is described in the Numerical Computation Guide.

6.2.1 –ftrap=mode Compiler Options

The -ftrap=mode option enables trapping for floating-point exceptions. If no signal handler has been established by an ieee_handler() call, the exception terminates the program with a memory dump core file. See the Fortran User’s Guide for details on this compiler option. For example, to enable trapping for overflow, division by zero, and invalid operations, compile with -ftrap=common. (This is the f95 default.)


Note –

You must compile the application’s main program with -ftrap= for trapping to be enabled.


6.2.2 Floating-Point Exceptions

f95 programs do not automatically report on exceptions. An explicit call to ieee_retrospective(3M) is required to display a list of accrued floating-point exceptions on program termination. In general, a message results if any one of the invalid, division-by-zero, or overflow exceptions have occurred. Inexact exceptions do not generate messages because they occur so frequently in real programs.

6.2.2.1 Retrospective Summary

The ieee_retrospective function queries the floating-point status registers to find out which exceptions have accrued and a message is printed to standard error to inform you which exceptions were raised but not cleared. The message typically looks like this; the format may vary with each compiler release:


Note: IEEE floating-point exception flags raised:
    Division by Zero;
IEEE floating-point exception traps enabled:
    inexact;  underflow;  overflow;  invalid operation;
See the Numerical Computation Guide, ieee_flags(3M),
    ieee_handler(3M)

A Fortran 95 program would need to call ieee_retrospective explicitly and compile with -xlang=f77 to link with the f77 compatibility library.

Compiling with the -f77 compatibility flag will enable the Fortran 77 convention of automatically calling ieee_retrospective at program termination.

You can turn off any or all of these messages with ieee_flags() by clearing exception status flags before the call to ieee_retrospective.

6.2.3 Handling Exceptions

Exception handling according to the IEEE standard is the default on SPARC and x86 processors. However, there is a difference between detecting a floating-point exception and generating a signal for a floating-point exception (SIGFPE).

Following the IEEE standard, two things happen when an untrapped exception occurs during a floating-point operation:

6.2.4 Trapping a Floating-Point Exception

f95 differs significantly from the earlier f77 compiler in the way it handles floating-point exceptions.

The default with f95 is to automatically trap on division by zero, overflow, and invalid operation. With f77, the default was not to automatically generate a signal to interrupt the running program for a floating-point exception. The assumption was that trapping would degrade performance while most exceptions were insignificant as long as expected values are returned.

The f95 command-line option -ftrap can be used to change the default. The default for f95 is -ftrap=common. To follow the earlier f77 default, compile the main program with -ftrap=%none.

6.2.5 Nonstandard Arithmetic

One aspect of standard IEEE arithmetic, called gradual underflow, can be manually disabled. When disabled, the program is considered to be running with nonstandard arithmetic.

The IEEE standard for arithmetic specifies a way of handling underflowed results gradually by dynamically adjusting the radix point of the significand. In IEEE floating-point format, the radix point occurs before the significand, and there is an implicit leading bit of 1. Gradual underflow allows the implicit leading bit to be cleared to 0 and shifts the radix point into the significand when the result of a floating-point computation would otherwise underflow. With a SPARC processor this result is not accomplished in hardware but in software. If your program generates many underflows (perhaps a sign of a problem with your algorithm), you may experience a performance loss.

Gradual underflow can be disabled either by compiling with the -fns option or by calling the library routine nonstandard_arithmetic() from within the program to turn it off. Call standard_arithmetic() to turn gradual underflow back on.


Note –

To be effective, the application’s main program must be compiled with -fns. See the Fortran User’s Guide.


For legacy applications, take note that: