C++ <cerrno> - EDOM constant
The C++ <cerrno> EDOM is macro constant which represents a domain error. The domain error occurs if an input argument is outside the domain of a mathematical function and sets errno to EDOM.
The domain of a mathematical function is the real value for which the function is defined. For example, the square root function is defined only for non-negative values, therefore when the sqrt() function is called with a negative argument it sets errno to EDOM.
The error messages associated with values of errno can be obtained using strerror() or directly printed using function perror().
In the <cerrno> header file, it is defined as follows:
#define EDOM /* implementation-defined */
Example 1:
The example below shows the usage of EDOM macro constant.
#include <iostream> #include <cmath> #include <cerrno> #include <cstring> using namespace std; int main(){ double result = sqrt(-1.0); cout<<result<<"\n"; if (errno == EDOM) { cout<<"sqrt(-1.0) failed: "<<strerror(errno)<<"\n"; } }
The output of the above code will be:
-nan sqrt(-1.0) failed: Numerical argument out of domain
Example 2:
Consider one more example where this macro constant is used to handle a domain error condition.
#include <cstdio> #include <cmath> #include <cerrno> #include <cstring> int main(){ double result = log(-1.0); printf("%f\n", result); if (errno == EDOM){ fprintf(stderr, "Value of errno: %d\n", errno); perror("Error printed by perror"); fprintf(stderr, "Error executing function: %s\n", strerror(errno)); } }
The output of the above code will be:
-nan Value of errno: 33 Error printed by perror: Numerical argument out of domain Error executing function: Numerical argument out of domain
❮ C++ <cerrno> Library