HP-UX Floating-Point Guide

132 Chapter 5
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
Sample Program: fe_round.c (cont.)
/*************************************************************/
x = -1.79e308;
y = 2.2e-308;
z = x / y; /* negative overflow */
printf("%g / %g = %g\n", x, y, z);
/* return to round-to-nearest */
fesetround(save_rnd);
rnd = fegetround();
if (rnd == FE_TONEAREST)
printf("rounding direction is FE_TONEAREST\n");
else
printf("unexpected rounding direction\n");
}
/*************************************************************/
If you compile and run this program, it produces the following output:
$ cc fe_round.c -lm
$ ./a.out
rounding direction is FE_TONEAREST
1.79e+308 / 2.2e-308 = inf
-1.79e+308 / 2.2e-308 = -inf
rounding direction is FE_TOWARDZERO
1.79e+308 / 2.2e-308 = 1.79769e+308
rounding direction is FE_UPWARD
-1.79e+308 / 2.2e-308 = -1.79769e+308
rounding direction is FE_TONEAREST
See “IEEE Rounding Modes” on page 54 for more information about
rounding modes on HP 9000 systems.
Exception Bits
The IEEE-754 standard specifies five floating-point exceptions:
divide-by-zero, overflow, underflow, inexact result, and invalid operation.
If one of these five exceptions occurs and the corresponding exception
trap enable bit is set to 0,the corresponding exception flag is set to 1
and no trap takes place. If an exception occurs and the exception trap
enable bit is set to 1,the trap takes place, and a SIGFPE signal is
generated. The exception trap enable bits are sometimes called mask
bits; the exception flags are sometimes called sticky bits. The term
sticky follows from the fact that once an exception flag is set by an
exception whose trap is disabled, it remains set for the life of the process
unless it is cleared by the application (for example, by a call to
fesetexceptflag or feclearexcept).