Main index | Section 3 | Options |
#include <fenv.h>
FE_DIVBYZERO | |
A divide-by-zero exception occurs when the exact result of a computation is infinite (according to the limit definition). For example, dividing a finite non-zero number by zero or computing log() raises a divide-by-zero exception. | |
FE_INEXACT | An inexact exception is raised whenever there is a loss of accuracy due to rounding. |
FE_INVALID | Invalid operation exceptions occur when a program attempts to perform calculations for which there is no reasonable representable answer. For instance, subtraction of like-signed infinities, division of zero by zero, ordered comparison involving NaNs, and taking the real square root of a negative number are all invalid operations. |
FE_OVERFLOW | In contrast with divide-by-zero, an overflow exception occurs when an infinity is produced because the magnitude of the exact result is finite but too large to fit in the destination type. For example, computing DBL_MAX * 2 raises an overflow exception. |
FE_UNDERFLOW | |
Underflow occurs when the result of a computation loses precision because it is too close to zero. The result is a subnormal number or zero. | |
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 | Results are rounded to the closest representable value. If the exact result is exactly half way between two representable values, the value whose last binary digit is even (zero) is chosen. This is the default mode. |
FE_DOWNWARD | Results are rounded towards negative ∞. |
FE_UPWARD | Results are rounded towards positive ∞. |
FE_TOWARDZERO | |
Results are rounded towards zero. | |
The fegetround() and fesetround() functions query and set the rounding mode.
The macro FE_DFL_ENV expands to a pointer to the default environment.
#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); }
#pragma STDC FENV_ACCESS ON
and disabled with the
#pragma STDC FENV_ACCESS OFF
directive. This lexically-scoped annotation tells the compiler that the program may access the floating-point environment, so optimizations that would violate strict IEEE-754 semantics are disabled. If execution reaches a block of code for which FENV_ACCESS is off, the floating-point environment will become undefined.
FENV (3) | March 16, 2005 |
Main index | Section 3 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | The wonderful thing about standards is that there are so many of them to choose from. | ” |
— Grace Murray Hopper |