FENV(3) | Library Functions Manual | FENV(3) |
feclearexcept
, fegetexceptflag
,
feraiseexcept
,
fesetexceptflag
, fetestexcept
,
fegetround
, fesetround
,
fegetenv
, feholdexcept
,
fesetenv
, feupdateenv
,
feenableexcept
,
fedisableexcept
, fegetexcept
—
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
int
feclearexcept
(int
excepts);
int
fegetexceptflag
(fexcept_t
*flagp, int
excepts);
int
feraiseexcept
(int
excepts);
int
fesetexceptflag
(const
fexcept_t *flagp, int
excepts);
int
fetestexcept
(int
excepts);
int
fegetround
(void);
int
fesetround
(int
round);
int
fegetenv
(fenv_t
*envp);
int
feholdexcept
(fenv_t
*envp);
int
fesetenv
(const
fenv_t *envp);
int
feupdateenv
(const
fenv_t *envp);
int
feenableexcept
(int
excepts);
int
fedisableexcept
(int
excepts);
int
fegetexcept
(void);
<fenv.h>
routines manipulate
the floating-point environment, which includes the exception flags and
rounding modes defined in IEEE Std 754-1985.
FE_DIVBYZERO
FE_INEXACT
FE_INVALID
FE_OVERFLOW
FE_UNDERFLOW
Additionally, the FE_ALL_EXCEPT
macro
expands to the bitwise OR of the above flags and any architecture-specific
flags. Combinations of these flags are passed to the
feclearexcept
(),
fegetexceptflag
(),
feraiseexcept
(),
fesetexceptflag
(), and
fetestexcept
() functions to clear, save, raise,
restore, and examine the processor's floating-point exception flags,
respectively.
Exceptions may be unmasked with
feenableexcept
() and masked with
fedisableexcept
(). Unmasked exceptions cause a trap
when they are produced, and all exceptions are masked by default. The
current mask can be tested with fegetexcept
().
FE_TONEAREST
FE_DOWNWARD
FE_UPWARD
FE_TOWARDZERO
The fegetround
() and
fesetround
() functions query and set the rounding
mode.
fegetenv
() 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 non-standard
mechanisms. 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 FE_DFL_ENV
expands to a pointer
to the default environment.
feraiseexcept
(). 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); }
<fenv.h>
conforms to ISO/IEC 9899:1999
(“ISO C99”). The
feenableexcept
(),
fedisableexcept
(), and
fegetexcept
() routines are extensions.
<fenv.h>
header first
appeared in FreeBSD 5.3 and NetBSD
6.0. It supersedes the non-standard routines defined in
<ieeefp.h>
and documented in
fpgetround(3).
#pragma STDC FENV_ACCESS
ON
#pragma STDC FENV_ACCESS
OFF
FENV_ACCESS
is off, the floating-point
environment will become undefined.
FENV_ACCESS
pragma is unimplemented in the system
compiler. However, non-constant expressions generally produce the correct
side-effects at low optimization levels.
March 16, 2005 | NetBSD 9.2 |