Oracle® Solaris Studio 12.4: Numerical Computation Guide

Exit Print View

Updated: January 2015
 
 

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 Solaris Studio 12.4 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 4–1 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 4-1  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)