gpt4 book ai didi

c - C语言中EDOM和ERANGE的区别?

转载 作者:行者123 更新时间:2023-12-05 09:25:51 25 4
gpt4 key购买 nike

errno 从标准库函数中捕获错误信息。但是,有两个宏值 EDOMERANGE 我觉得很难区分。

来自 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 become EDOM, and what operations will cause it to become ERANGE?

These are math functions that can be graphed: y = f(x). Invalid values of the independent variable x will produce "domain" errors. Invalid values of the dependent variable y will produce "range" errors. @Fe2O3

EDOM means the mathematical function is not defined there, and ERANGE means the computed function cannot give you the mathematical result because it cannot be represented in the result format. @Eric Postpischil


EDOMERANGE 都是在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com