HP-UX Floating-Point Guide
144 Chapter 5
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
Sample Program: fe_update.c (cont.)
/*************************************************************/
feholdexcept(&holdenv);
/* accumulate sum of squares */
for (i = 0; i < ARRLEN; i++) {
z = argarr[i] * argarr[i];
y +=z;
}
set_excepts = fetestexcept(FE_ALL_EXCEPT);
print_flags(set_excepts);
printf("sum of squares is %g\n", y);
fegetenv(&env);
printf("after more calculations, env is %08x\n", env);
if (y >= DBL_MIN) {
printf("clearing underflow & inexact exceptions\n");
feclearexcept(FE_UNDERFLOW | FE_INEXACT);
}
else {
printf("clearing inexact exception\n");
feclearexcept(FE_INEXACT);
}
fegetenv(&env);
printf("after feclearexcept, env is %08x\n", env);
printf("merging env with saved version\n");
feupdateenv(&holdenv);
fegetenv(&env);
printf("after feupdateenv, env is %08x\n", env);
set_excepts = fetestexcept(FE_ALL_EXCEPT);
print_flags(set_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");
}
/*************************************************************/