C <math.h> - math_errhandling, MATH_ERRNO, MATH_ERREXCEPT
The C <math.h> math_errhandling macro constant expands to an expression of type int that is either equal to MATH_ERRNO, or equal to MATH_ERREXCEPT, or equal to their bitwise OR (MATH_ERRNO | MATH_ERREXCEPT). The value of math_errhandling indicates the type of error handling that is performed by the floating-point operators and functions.
Constant | Value | Description |
---|---|---|
MATH_ERRNO | 1 | errno is used to signal errors: |
MATH_ERREXCEPT | 2 | The C exception is raised:
|
MATH_ERRNO | MATH_ERREXCEPT | 3 | Both of the above |
Definition in the <math.h> header file is:
#define MATH_ERRNO 1 #define MATH_ERREXCEPT 2 #define math_errhandling /* implementation defined */
Example:
The example below shows the usage of following macros.
#include <stdio.h> #include <fenv.h> #include <math.h> #include <errno.h> #include <string.h> #include <float.h> #pragma STDC FENV_ACCESS ON int main (){ printf("MATH_ERRNO is %s\n", (math_errhandling & MATH_ERRNO ? "set" : "not set")); printf("MATH_ERREXCEPT is %s\n", (math_errhandling & MATH_ERREXCEPT ? "set" : "not set")); errno = 0; printf("\nlog(0) = %f\n", log(0)); if(errno == ERANGE) printf("errno = ERANGE ( %s )\n", strerror(errno)); if(fetestexcept(FE_DIVBYZERO)) printf("FE_DIVBYZERO (pole error) reported\n"); return 0; }
The output of the above code will be:
MATH_ERRNO is set MATH_ERREXCEPT is set log(0) = -inf errno = ERANGE ( Numerical result out of range ) FE_DIVBYZERO (pole error) reported
❮ C <math.h> Library