HP-UX Floating-Point Guide

Chapter 5 131
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
Sample Program: fe_round.c
/*************************************************************/
#include <stdio.h>
#include <fenv.h>
int main(void)
{
int save_rnd, rnd;
double x, y, z;
save_rnd = fegetround();
if (save_rnd == FE_TONEAREST)
printf("rounding direction is FE_TONEAREST\n");
else
printf("unexpected rounding direction\n");
x = 1.79e308;
y = 2.2e-308;
z = x / y; /* overflow */
printf("%g / %g = %g\n", x, y, z);
x = -1.79e308;
y = 2.2e-308;
z = x / y; /* negative overflow */
printf("%g / %g = %g\n", x, y, z);
fesetround(FE_TOWARDZERO);
rnd = fegetround();
if (rnd == FE_TOWARDZERO)
printf("rounding direction is FE_TOWARDZERO\n");
else
printf("unexpected rounding direction\n");
x = 1.79e308;
y = 2.2e-308;
z = x / y; /* overflow */
printf("%g / %g = %g\n", x, y, z);
fesetround(FE_UPWARD);
rnd = fegetround();
if (rnd == FE_UPWARD)
printf("rounding direction is FE_UPWARD\n");
else
printf("unexpected rounding direction\n");
/* continued */
/*************************************************************/