HP-UX Floating-Point Guide

138 Chapter 5
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
Sample Program: fe_traps.c (cont.)
/*************************************************************/
void print_flags(int flags)
{
if (flags & FE_INEXACT)
printf(" inexact result flag set\n");
if (flags & FE_UNDERFLOW)
printf(" underflow flag set\n");
if (flags & FE_OVERFLOW)
printf(" overflow flag set\n");
if (flags & FE_DIVBYZERO)
printf(" division by zero flag set\n");
if (flags & FE_INVALID)
printf(" invalid operation flag set\n");
if (!(flags & FE_ALL_EXCEPT))
printf(" no exception flags are set\n");
}
void print_traps(int traps)
{
if (traps & FE_INEXACT)
printf(" inexact result trap enabled\n");
if (traps & FE_UNDERFLOW)
printf(" underflow trap enabled\n");
if (traps & FE_OVERFLOW)
printf(" overflow trap enabled\n");
if (traps & FE_DIVBYZERO)
printf(" division by zero trap enabled\n");
if (traps & FE_INVALID)
printf(" invalid operation trap enabled\n");
if (!(traps & FE_ALL_EXCEPT))
printf(" no traps are enabled\n");
}
/*************************************************************/
If you run this program, it produces the following output:
$ cc fe_traps.c -lm
$ ./a.out
at start:
no traps are enabled
no exception flags are set
after fesettrapenable(FE_DIVBYZERO):
division by zero trap enabled
raising division by zero exception now
Raised signal 8 -- floating point error
division by zero trap enabled
no exception flags are set