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

标签 c errno

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/

相关文章:

c - 自纪元以来的秒数的星期几

c++ - char * 是什么意思

mysql - #1005 - 无法创建表...(错误号 : 150)

c - 为什么从数据集的/dev 源成功读取 errno?

MySQL InnoDB 外键问题

python - Pandas DataFrame.to_csv() OSError : [Errno 22] Invalid argument and PermissionError: [Errno 13] Permission denied

c - 跟踪 C 阻塞系统调用

c++ - 通过 WinSocket 客户端/服务器应用程序重用套接字

c - 使用 Arduino Mega 2560 - RS232 从条码扫描仪读取 Tx

c - VxWorks PPC 中 errno 始终返回零