SYNOPSIS
#include <fenv.h>
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)
DESCRIPTION
The <fenv.h> routines manipulate the floating-point environment, which includes the exception flags and rounding modes defined in IEEE Std 754-1985.
Exceptions
Exception 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.
- FE_DIVBYZERO
-
Is the location where the resulting sign is stored. 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(0) 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
Exceptions may be unmasked with
Rounding Modes
IEEE 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.
- 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 infinity.
- FE_UPWARD
-
Results are rounded towards positive infinity.
- FE_TOWARDZERO
-
Results are rounded towards zero.
The
Environment Control
The
The macro FE_DFL_ENV expands to a pointer to the default environment.
RETURN VALUES
All the other functions return zero for success and non-zero for failure.
EXAMPLE
The following routine computes the square root function. It explicitly
raises an invalid exception on appropriate inputs using
#define _BSD_SOURCE /* for fenv extensions */ #include <float.h> #include <math.h> #include <fenv.h> double fsqrt(double n) { double x = 1.0; fenv_t env; if (isnan(n) || n < 0.0) { /* If you expect this to throw an exception (SIGFPE) */ /* call feenableexcept(FE_ALL_EXCEPT); somewhere */ 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); }
CONFORMANCE
Except as noted below, <fenv.h> conforms to ANSI/ISO 9899:1999 'ISO C99'.
The
MULTITHREAD SAFETY LEVEL
Unsafe.
PORTING ISSUES
None.
AVAILABILITY
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
SEE ALSO
- Functions:
math()
PTC MKS Toolkit 10.4 Documentation Build 39.