errno
从标准库函数中捕获错误信息。但是,有两个宏值 EDOM
和 ERANGE
我觉得很难区分。
来自 cppreference 的示例代码将执行以下操作
#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;
}
它会产生
domain error occurred
pole or range error occurred
所以我的问题是:哪些操作会导致errno
变成EDOM
,哪些操作会导致它变成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
或
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
EDOM
和 ERANGE
都是在 C 标准中定义的。
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
例如ojit_代码
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
例如ojit_代码
关于c - C语言中EDOM和ERANGE的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75000827/