C++ <cmath> - math_errhandling, MATH_ERRNO, MATH_ERREXCEPT
The C++ <cmath> 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 <cmath> 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 <iostream> #include <cfenv> #include <cmath> #include <cerrno> #include <cstring> #include <cfloat> #pragma STDC FENV_ACCESS ON using namespace std; int main (){ cout<<"MATH_ERRNO is " <<(math_errhandling & MATH_ERRNO ? "set" : "not set")<<endl; cout<<"MATH_ERREXCEPT is " <<(math_errhandling & MATH_ERREXCEPT ? "set" : "not set")<<endl; errno = 0; cout<<"\nlog(0) = "<<log(0)<<endl; if(errno == ERANGE) cout<<"errno = ERANGE ("<<strerror(errno)<<")\n"; if(fetestexcept(FE_DIVBYZERO)) cout<<"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++ <cmath> Library