HP-UX Floating-Point Guide
Chapter 5 141
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
Sample Program: fe_env.c (cont.)
/*************************************************************/
feholdexcept(&holdenv);
do {
printf("\nEnter x and y: ");
scanf("%lf %lf", &x, &y);
printf("x and y are %g and %g\n", x, y);
z = x/y; /* perform calculations */
set_excepts = fetestexcept(FE_ALL_EXCEPT);
print_flags(set_excepts);
printf("result is %g\n", z);
printf("again? (y or n) ");
fflush(stdin);
scanf("%c", &c);
} while (c != 'n');
fegetenv(&env);
printf("after more calculations, env is %08x\n", env);
printf("resetting env to saved version\n");
fesetenv(&holdenv);
set_excepts = fetestexcept(FE_ALL_EXCEPT);
print_flags(set_excepts);
fegetenv(&env);
printf("env is %08x\n", env);
feclearexcept(FE_UNDERFLOW | FE_INEXACT);
fegetenv(&env);
printf("after feclearexcept(FE_UNDERFLOW | FE_INEXACT), \
env is %08x\n", env);
set_excepts = fetestexcept(FE_ALL_EXCEPT);
print_flags(set_excepts);
fesetenv(FE_DFL_ENV);
fegetenv(&env);
printf("after fesetenv(FE_DFL_ENV), env is %08x\n", env);
}
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");
}
/*************************************************************/