errno
captures error messages from standard library functions. However, there are two macro values EDOM
and ERANGE
which I find hard to distinguish.
An example code from cppreference will do the following
#include <stdio.h>
#include <math.h>
#include <errno.h>
void show_errno(void)
{
const char *err_info = "unknown error";
switch (errno) {
case EDOM:
err_info = "domain error";
break;
case EILSEQ:
err_info = "illegal sequence";
break;
case ERANGE:
err_info = "pole or range error";
break;
case 0:
err_info = "no error";
}
fputs(err_info, stdout);
puts(" occurred");
}
int main(void)
{
errno = 0;
acos(+1.1);
show_errno();
errno = 0;
log(0.0);
show_errno();
return 0;
}
and it will produce
domain error occurred
pole or range error occurred
So my question is: what operations will cause errno
to become EDOM
, and what operations will cause it to become ERANGE
?
... what operations will cause
errno
to becomeEDOM
, and what operations will cause it to becomeERANGE
?
These are math functions that can be graphed:
y = f(x)
. Invalid values of the independent variablex
will produce "domain" errors. Invalid values of the dependent variabley
will produce "range" errors. @Fe2O3
or
EDOM
means the mathematical function is not defined there, andERANGE
means the computed function cannot give you the mathematical result because it cannot be represented in the result format. @Eric Postpischil
Both EDOM
and ERANGE
are defined in the C standard.
For all functions (
<math.h>
), a domain error occurs if and only if an input argument is outside the domain over which the mathematical function is defined. C17dr § 7.12.1 2
E.g. sqrt(-1)
Likewise, a range error occurs if and only if the mathematical result of the function cannot be represented in an object of the specified type, due to extreme magnitude. § 7.12.1 4
E.g. strtol("1234567890123456789012345678901234567890", 0, 0)