# The FloatMode interface

The `FloatMode` interface allows you to test the behavior of rounding and of numerical exceptions. On some implementations it also allows you to change the behavior, on a per-thread basis.

For definitions of the terms used in this interface, see the ANSI/IEEE Standard 754-1985 for floating-point arithmetic.

    INTERFACE FloatMode;

    CONST IEEE: BOOLEAN = ...;

`TRUE` for fully-compliant IEEE implementations.

    EXCEPTION Failure;

Raised by attempts to set modes that are not supported by the implementation.

    TYPE RoundingMode =
      {NearestElseEven, TowardMinusInfinity, TowardPlusInfinity,
       TowardZero, NearestElseAwayFromZero, IBM370, Other};

Rounding modes. The first four are the IEEE modes.

    CONST RoundDefault: RoundingMode = ...;

Implementation-dependent: the default mode for rounding arithmetic operations, used by a newly forked thread. This also specifies the behavior of the `ROUND` operation in half-way cases.

    PROCEDURE SetRounding(md: RoundingMode) RAISES {Failure};

Change the rounding mode for the calling thread to `md`, or raise the exception if this cannot be done. This affects the implicit rounding in floating-point operations; it does not affect the `ROUND` operation. Generally this can be done only on IEEE implementations and only if `md` is an IEEE mode.

    PROCEDURE GetRounding(): RoundingMode;

Return the rounding mode for the calling thread.

    TYPE Flag =
         {Invalid, Inexact, Overflow, Underflow,
          DivByZero, IntOverflow, IntDivByZero};

Associated with each thread is a set of boolean status flags recording whether the condition represented by the flag has occurred in the thread since the flag was last reset. The meaning of the first five flags is defined precisely in the IEEE floating point standard; roughly they mean:

> `Invalid` = invalid argument to an operation.

> `Inexact` = an operation produced an inexact result.

> `Overflow` = a floating-point operation produced a result whose absolute value is too large to be represented.

> `Underflow` = a floating-point operation produced a result whose absolute value is too small to be represented.

> `DivByZero` = floating-point division by zero.

The meaning of the last two flags is:

> `IntOverflow` = an integer operation produced a result whose absolute value is too large to be represented.

> `IntDivByZero` = integer `DIV` or `MOD` by zero.

    CONST NoFlags = SET OF Flags {};

    PROCEDURE GetFlags(): SET OF Flag;

Return the set of flags for the current thread.

    PROCEDURE SetFlags(s: SET OF Flag): SET OF Flag RAISES{Failure};

Set the flags for the current thread to `s`, and return their previous values.

    PROCEDURE ClearFlag(f: Flag);

Turn off the flag `f` for the current thread.

    EXCEPTION
      Trap(Flag);

    TYPE
      Behavior = {Trap, SetFlag, Ignore};

The behavior of an operation that causes one of the flag conditions is either

> `Ignore` = return some result and do nothing.

> `SetFlag` = return some result and set the condition flag. For IEEE implementations, the result of the operation is defined by the standard.

> `Trap` = possibly set the condition flag; in any case raise the `Trap` exception with the appropriate flag as the argument.

    PROCEDURE SetBehavior(f: Flag; b: Behavior) RAISES {Failure};

Set the behavior of the current thread for the flag `f` to be `b`, or raise `Failure` if this cannot be done.

    PROCEDURE GetBehavior(f: Flag): Behavior;

Return the behavior of the current thread for the flag `f`.

    END FloatMode.

    Last modified on Wed Apr  3 09:39:06 PST 1996 by heydon
         modified on Thu Jun  1 08:10:29 PDT 1995 by kalsow