HP-UX Floating-Point Guide

Chapter 5 135
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
Sample Program: fe_flags.c (cont.)
/*************************************************************/
/* 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);
/* raise inexact exception */
feraiseexcept(FE_INEXACT);
excepts = fetestexcept(FE_DIVBYZERO | FE_INEXACT);
printf("after raising inexact exception:\n");
print_flags(excepts);
/* clear exceptions, retrieve new setting */
feclearexcept(FE_ALL_EXCEPT);
excepts = fetestexcept(FE_ALL_EXCEPT);
printf("after clearing exceptions:\n");
print_flags(excepts);
/* raise underflow exception; inexact also set */
feraiseexcept(FE_UNDERFLOW);
excepts = fetestexcept(FE_ALL_EXCEPT);
printf("after raising underflow exception:\n");
print_flags(excepts);
/* restore original state of flags */
fesetexceptflag(&flags, FE_ALL_EXCEPT);
printf("after fesetexceptflag:\n");
excepts = fetestexcept(FE_ALL_EXCEPT);
print_flags(excepts);
}
void print_flags(int flags)
{
if (flags & FE_INEXACT)
printf(" inexact result occurred\n");
if (flags & FE_UNDERFLOW)
printf(" underflow occurred\n");
if (flags & FE_OVERFLOW)
printf(" overflow occurred\n");
if (flags & FE_DIVBYZERO)
printf(" division by zero occurred\n");
if (flags & FE_INVALID)
printf(" invalid operation occurred\n");
if (!(flags & FE_ALL_EXCEPT))
printf(" no exceptions are set\n");
}
/*************************************************************/