|
NAMEfeclearexcept , fegetexceptflag ,
feraiseexcept ,
fesetexceptflag , fetestexcept ,
fegetround , fesetround ,
fegetenv , feholdexcept ,
fesetenv , feupdateenv ,
feenableexcept ,
fedisableexcept , fegetexcept
—
floating-point environment control
LIBRARYMath Library (libm, -lm)SYNOPSIS#include <fenv.h>
int
int
int
int
int
int
int
int
int
int
int
int
int
int
DESCRIPTIONThe<fenv.h> routines manipulate
the floating-point environment, which includes the exception flags and
rounding modes defined in IEEE Std 754-1985.
ExceptionsException flags are set as side-effects of floating-point arithmetic operations and math library routines, and they remain set until explicitly cleared. The following macros expand to bit flags of type int representing the five standard floating-point exceptions.
Additionally, the Exceptions may be unmasked with
Rounding ModesIEEE Std 754-1985 specifies four rounding modes. These modes control the direction in which results are rounded from their exact values in order to fit them into binary floating-point variables. The four modes correspond with the following symbolic constants.
The Environment ControlThefegetenv () and fesetenv ()
functions save and restore the floating-point environment, which includes
exception flags, the current exception mask, the rounding mode, and possibly
other implementation-specific state. The
feholdexcept () function behaves like
fegetenv (), but with the additional effect of clearing
the exception flags and installing a non-stop mode. In
non-stop mode, floating-point operations will set exception flags as usual,
but no SIGFPE signals will be generated as a result.
Non-stop mode is the default, but it may be altered by
feenableexcept () and
fedisableexcept (). The
feupdateenv () function restores a saved environment
similarly to fesetenv (), but it also re-raises any
floating-point exceptions from the old environment.
The macro EXAMPLESThe following routine computes the square root function. It explicitly raises an invalid exception on appropriate inputs usingferaiseexcept (). It also defers inexact exceptions
while it computes intermediate values, and then it allows an inexact exception
to be raised only if the final answer is inexact.
#pragma STDC FENV_ACCESS ON double sqrt(double n) { double x = 1.0; fenv_t env; if (isnan(n) || n < 0.0) { feraiseexcept(FE_INVALID); return (NAN); } if (isinf(n) || n == 0.0) return (n); feholdexcept(&env); while (fabs((x * x) - n) > DBL_EPSILON * 2 * x) x = (x / 2) + (n / (2 * x)); if (x * x == n) feclearexcept(FE_INEXACT); feupdateenv(&env); return (x); } SEE ALSOcc(1), feclearexcept(3), fedisableexcept(3), feenableexcept(3), fegetenv(3), fegetexcept(3), fegetexceptflag(3), fegetround(3), feholdexcept(3), feraiseexcept(3), fesetenv(3), fesetexceptflag(3), fesetround(3), fetestexcept(3), feupdateenv(3), fpgetprec(3), fpsetprec(3)STANDARDSExcept as noted below,<fenv.h>
conforms to ISO/IEC 9899:1999
(“ISO C99”). The
feenableexcept (),
fedisableexcept (), and
fegetexcept () routines are extensions.
HISTORYThe<fenv.h> header first
appeared in FreeBSD 5.3. It supersedes the
non-standard routines defined in
<ieeefp.h> and documented in
fpgetround(3).
CAVEATSThe FENV_ACCESS pragma can be enabled with#pragma STDC FENV_ACCESS
ON #pragma STDC FENV_ACCESS
OFF FENV_ACCESS is off, the floating-point
environment will become undefined.
BUGSTheFENV_ACCESS pragma is unimplemented in the system
compiler. However, non-constant expressions generally produce the correct
side-effects at low optimization levels.
Visit the GSP FreeBSD Man Page Interface. |