HP-UX Floating-Point Guide

140 Chapter 5
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
The following program shows the use of all these functions except
feupdateenv.
Sample Program: fe_env.c
/*************************************************************/
#include <stdio.h>
#include <fenv.h>
int main(void)
{
double x, y, z;
fenv_t env, holdenv;
int set_excepts;
fexcept_t flags;
char c = ' ';
void print_flags(int);
fegetenv(&env);
printf("at start, env is %08x\n", env);
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');
printf("enabling trap for inexact\n");
fesettrapenable(FE_INEXACT);
fegetenv(&env);
printf("after calculations and enabling inexact trap, \
env is %08x\n", env);
printf("saving environment\n");
/* continued */
/*************************************************************/