Go to main content
Oracle® Developer Studio 12.6: Numerical Computation Guide

Exit Print View

Updated: July 2017
 
 

4.2 What Is an Exception?

It is hard to define exceptions. To quote W. Kahan,

An arithmetic exception arises when an attempted atomic arithmetic operation has no result that would be acceptable universally. The meanings of atomic and acceptable vary with time and place. (See Handling Arithmetic Exceptions by W. Kahan.)

For example, an exception arises when a program attempts to take the square root of a negative number. This example is one case of an invalid operation exception. When such an exception occurs, the system responds in one of two ways:

  • If the exception's trap is disabled (the default case), the system records the fact that the exception occurred and continues executing the program using the default result specified by IEEE 754 for the excepting operation.

  • If the exception's trap is enabled, the system generates a SIGFPE signal. If the program has installed a SIGFPE signal handler, the system transfers control to that handler; otherwise, the program aborts.

IEEE 754 defines five basic types of floating-point exceptions: invalid operation, division by zero, overflow, underflow and inexact. The first three (invalid, division, and overflow) are sometimes collectively called common exceptions. These exceptions can seldom be ignored when they occur. The ieee_handler(3M) man page explains an easy way to trap on common exceptions only. The other two exceptions (underflow and inexact) are seen more often. In fact, most floating-point operations incur the inexact exception. These exceptions can usually, though not always, be safely ignored. Oracle Developer Studio 12.6 C, C++, and f77 compilers disable all IEEE traps by default. The f95 compiler enables traps for the common exceptions by default. 754-standard conforming can be restored by compiling with f95 –ftrap=none.

Table 31 condenses information found in IEEE Standard 754. It describes the five floating-point exceptions and the default response of an IEEE arithmetic environment when these exceptions are raised.

Table 31  IEEE Floating-Point Exceptions
IEEE
Exception
Reason Why This Arises
Example
Default Result When
Trap is Disabled
Invalid operation
An operand is invalid for the operation about to be performed.
(On x86, this exception is also raised when the floating-point stack underflows or overflows, though that is not part of the IEEE standard.)
  • 0 × ∞

  • 0 / 0

  • ∞ / ∞

  • x REM 0

  • Square root of negative operand

  • Any operation with a signaling NaN operand

  • Unordered comparison (see note 1)

  • Invalid conversion (see note 2)

Quiet NaN
Division by zero
An exact infinite result is produced by an operation on finite operands.
  • x / 0 for finite, nonzero x

  • log(0)

Correctly signed infinity
Overflow
The correctly rounded result would be larger in magnitude than the largest finite number representable in the destination format (i.e., the exponent range is exceeded).
  • Double precision:

    • DBL_MAX + 1.0e294

    • exp(709.8)

  • Single precision:

    • (float)DBL_MAX

    • FLT_MAX + 1.0e32

    • expf(88.8)

Depends on rounding mode (RM), and the sign of the intermediate result. See item 4 in Notes for Table 4-1.
Underflow
Either the exact result or the correctly rounded result would be smaller in magnitude than the smallest normal number representable in the destination format (see note 3).
  • Double precision:

    • nextafter(min_normal,-•)

    • nextafter(min_subnormal,-•)

    • DBL_MIN §3.0

    • exp(-708.5)

  • Single precision:

    • (float)DBL_MIN

    • nextafterf(FLT_MIN, -•)

    • expf(-87.4)

Subnormal or zero
Inexact
The rounded result of a valid operation is different from the infinitely precise result. (Most floating-point operations raise this exception.)
  • 2.0 / 3.0

  • (float)1.12345678

  • log(1.1)

  • DBL_MAX + DBL_MAX, when no overflow trap

The result of the operation (rounded, overflowed, or underflowed)

4.2.1 Notes for Table 4-1

  1. Unordered comparison: Any pair of floating-point values can be compared, even if they are not of the same format. Four mutually exclusive relations are possible: less than, greater than, equal, or unordered. Unordered means that at least one of the operands is a NaN (not a number).

    Every NaN compares “unordered” with everything, including itself. The following table shows which predicates cause the invalid operation exception when the relation is unordered.

    Math Predicate
    C, C++ Predicate
    Fortran Predicate
    Invalid Expression (If Unordered)
    =
    ==
    .EQ.
    no
    !=
    .NE.
    no
    >
    >
    .GT.
    yes
    >=
    .GE.
    yes
    <
    <
    .LT.
    yes
    <=
    .LE.
    yes
  2. Invalid conversion: Attempt to convert NaN or infinity to an integer, or integer overflow on conversion from floating-point format.

  3. The smallest normal numbers representable in the IEEE single, double, and extended formats are 2-126, 2-1022, and 2-16382, respectively. See IEEE Arithmetic for a description of the IEEE floating-point formats.

  4. The following table lists the default result when the trap is disabled for overflow. The below results depend on the rounding mode and the sign of the intermediate result.

    Rounding Mode
    Positive
    Negative
    Nearest
    +∞
    -∞
    Zero
    +∞
    -max
    Down
    +max
    -∞
    Up
    +∞
    -max

The x86 floating-point environment provides another exception not mentioned in the IEEE standards: the denormal operand exception. This exception is raised whenever a floating-point operation is performed on a subnormal number.

Exceptions are prioritized in the following order: invalid (highest priority), overflow, division, underflow, inexact (lowest priority). On x86-based systems, the denormal operand exception has the lowest priority of all.

The only combinations of standard exceptions that can occur simultaneously in a single operation are overflow with inexact and underflow with inexact. On x86-based systems, the denormal operand exception can occur with any of the five standard exceptions. If trapping on overflow, underflow, and inexact is enabled, the overflow and underflow traps take precedence over the inexact trap; they all take precedence over a denormal operand trap on x86-based systems.