HP-UX Floating-Point Guide

Chapter 5 143
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
Sample Program: fe_update.c
/*************************************************************/
#include <stdio.h>
#include <float.h>
#include <fenv.h>
#define ARRLEN 11
static double argarr[ARRLEN] = { -1.73205, -0.57735,
0.0174551, 0.57735, 1.0, 1.73205,
2.0, -1.22465e-244,
-2.44929e-207, -1.0 };
int main(void)
{
double x, y, z;
fenv_t env, holdenv;
int set_excepts, i;
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("setting traps for overflow and underflow\n");
fesettrapenable(FE_OVERFLOW | FE_UNDERFLOW);
fegetenv(&env);
printf("after calculations, env is %08x\n", env);
printf("saving environment\n");
/* continued */
/*************************************************************/