HP-UX Floating-Point Guide
134 Chapter 5
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
The fesetexceptflag function sets the status for the exception flags
indicated by the argument excepts according to the representation in the
object pointed to by flagp. Use it to reset the exception flags to a
previously saved state.
The fetestexcept function determines which of a specified subset of
the exception flags are currently set.
The feraiseexcept function raises the exceptions represented by its
argument (and causes traps if the corresponding traps are enabled). It
allows you to raise exceptions directly without having to write operations
in your program that generate an exception.
The feclearexcept function clears the exception flags represented by
its argument.
The following program uses all these functions. First it calls
fegetexceptflag to retrieve the initial settings of the exception flags.
Then it calls feraiseexcept to raise a couple of exceptions, and calls
fetestexcept to verify that the flags are set correctly. Then it calls
feclearexcept to clear the accumulated flags, and after another call to
fetestexcept it calls feraiseexcept again to set the underflow
exception. The next call to fetestexcept shows that, as with basic
operations, raising the underflow exception also raises the inexact
exception on HP systems. Finally, the program calls fesetexceptflag
to restore the initial state of the exception flags.
Sample Program: fe_flags.c
/*************************************************************/
#include <stdio.h>
#include <fenv.h>
int main(void)
{
fexcept_t flags;
int excepts;
void print_flags(int);
fegetexceptflag(&flags, FE_ALL_EXCEPT);
printf("at start:\n");
print_flags(flags);
/* raise divide-by-zero exception */
feraiseexcept(FE_DIVBYZERO);
excepts = fetestexcept(FE_DIVBYZERO | FE_INEXACT);
printf("after raising divide-by-zero exception:\n");
print_flags(excepts);
/* continued */
/*************************************************************/