Friday, July 24, 2015

Floating-point environment(fenv)

The type fenv_t represents the entire floating-point environment.
The type fexcept_t represents the floating-point status flags collectively, including any status the implementation associates with the flags.

a function call is assumed to require default floating-point control modes, unless its
documentation promises otherwise(The macro FE_DFL_ENV
represents the default floating-point environment — the one installed at program startup
— and has type ‘‘pointer to const-qualified fenv_t’’).

The FENV_ACCESS pragma provides a means to inform the implementation when a
program might access the floating-point environment to test floating-point status flags or
run under non-default floating-point control modes.The FENV_ACCESS macro is scopable(zxxu),for eaxmple,When inside a compound statement, the pragma takes effect from its occurrence until another FENV_ACCESS pragma is encountered (including within a
nested compound statement), or until the end of the compound statement).

EXAMPLE:
#include <fenv.h>
void f(double x)
{
#pragma STDC FENV_ACCESS ON
void g(double);
void h(double);
/* ... */
g(x + 1);
h(x + 1);
/* ... */
}

the function g might depend on status flags set as a side effect of the first x + 1, or the second
x + 1 might depend on control modes set as a side effect of the call to function g, and so on.

EXAMPLE:Call f if ‘‘invalid’’ is set, then g if ‘‘overflow’’ is set:
#include <fenv.h>
/* ... */
{
#pragma STDC FENV_ACCESS ON
int set_excepts;
feclearexcept(FE_INVALID | FE_OVERFLOW);
// maybe raise exceptions
set_excepts = fetestexcept(FE_INVALID | FE_OVERFLOW);
if (set_excepts & FE_INVALID) f();
if (set_excepts & FE_OVERFLOW) g();
/* ... */
}

No comments:

Post a Comment