Exception Flags

Compliant libraries offer support for exception flags, according to the IEEE754 standard. For each exception, the implementation provides a status flag that is set when the exception occurs. The flag is cleared only at the user's request. The user can test and alter flags individually, or several at a time. Furthermore, all flags can be saved and restored.

There are five supported exceptions:

  1. invalid operation: Signaled if an operand is invalid for the operation. For example: Inf - Inf, 0 * Inf, 0 / 0, Inf / Inf and others. The result will be a NaN.
  2. division by zero: Signaled if the divisor is 0 and the dividend is a finite non-zero. The result will be a correctly signed Inf.
  3. overflow: Signaled when the destination format's largest finite number is exceeded by the rounded result. The result will be a +Inf.
  4. underflow: Signaled when a tiny (between +/- 2Emin) non-zero result is created. The result will be 0.
  5. inexact: Signaled when the result of an operation is not exact, including the case of overflow.

The five exception flags are defined in except.h: FP_EINVALID, FP_EDIVZERO, FP_EOVERFLOW, FP_EUNDERFLOW, FP_EINEXACT.

For example, in order to check if an operation has overflowed, the testFlags function is used:

if (testFlags(FP_EOVERFLOW)) { do_something(); }

Multiple flags can be checked at the same time:

if (testFlags(FP_EOVERFLOW | FP_EUNDERFLOW)) { do_something(); }

Flags that have been set by a certain floating point operation are not cleared by subsequent operations. The only way to clear flags is by using the lower_flags function:

lowerFlags(FP_INVALID | FP_EOVERFLOW | FP_EUNDERFLOW)

Finally, flags can be saved using the saveAllFlags() function. Any number and combination of flags can be restored to their previous state using the restoreFlags(char, char) function.