The Floating-Point Environment

ANSI C99 has introduced the floating-point environment to permit more detailed representation of error conditions in floating-point arithmetic. All of the declarations for the floating-point environment are contained in the header file fenv.h(*). The floating-point environment contains two system variables: one for the status flags, which are used in handling floating-point exceptions, and one for the control modes , which determine certain behaviors of floating-point arithmetic, such as the rounding method used.

For every exception possible in an implementation that supports floating-point exceptions, an appropriate status flag is defined, as described in Table 1-37.

Table 1-37. Macros for floating-point exceptions in fenv.h(*)

Macro

Error condition

FE_DIVBYZERO

Division by 0

FE_INEXACT

The result of the operation is not exact

FE_INVALID

The result is undefined, e.g., a value was outside the domain for which the function is defined

FE_OVERFLOW

A floating-point overflow occurred

FE_UNDERFLOW

An underflow occurred

Several of these constants can be combined by a bitwise OR (|). The macro FE_ALL_EXCEPT is equal to the bitwise OR of all of the floating-point exception constants implemented. The system variable for the floating-point exception status has the type fexcept_t .

The following functions are used to handle floating-point exceptions. With the exception of fetestexcept(), each function returns 0 to indicate success, or a value ...

Get C Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.