HP-UX Floating-Point Guide
142 Chapter 5
Manipulating the Floating-Point Status Register
Run-Time Mode Control: The fenv(5) Suite
If you compile and run this program, the call to fesetenv restores the
environment without raising the inexact exception, so the inexact trap is
not taken even though it is set.
$ fe_env
at start, env is 000b0800
Enter x and y: 1.0e308 1.0e-308
x and y are 1e+308 and 1e-308
inexact result occurred
overflow occurred
result is inf
again? (y or n) n
setting trap for inexact
after calculations and enabling inexact trap, env is 280b0801
saving environment
Enter x and y: 1.0e-308 1.0e308
x and y are 1e-308 and 1e+308
inexact result occurred
underflow occurred
result is 0
again? (y or n) n
after more calculations, env is 1c0b0800
resetting env to saved version
inexact result occurred
overflow occurred
env is 2c0b0801
after feclearexcept(FE_UNDERFLOW | FE_INEXACT), env is 240b0801
overflow occurred
after fesetenv(FE_DFL_ENV), env is 040b0800
The following program illustrates the use of the feupdateenv
function.This program computes a sum of squares. Some of the
intermediate computations may underflow, but only if the final result
underflows or overflows do we want to raise an exception. Therefore,
after setting traps for underflow and overflow exceptions, the program
calls feholdexcept to save the previously accumulated exceptions.
After performing the intermediate computations, it clears any underflow
or inexact exceptions, then calls feupdateenv to merge the current
exceptions with the previously accumulated ones.