呃不 是用于错误指示的预处理器宏。
null
- 价值 呃不 在程序启动时设置为零,并且允许标准C++库的任何函数对ErNO进行正整数的写入,而不管是否发生错误。
- 一旦ErNO的值从零变为非零,那么C++标准库中的其他函数就不能将其值更改为零。errno在中定义 塞尔诺 头文件。
- 当数学参数中存在错误时,errno的值设置为33。在C++中,数学论证的误差由 以东 其值为33。
声明errno()的头还声明了至少以下值不同于零的宏常量:
- EDOM–域错误 :一些数学函数只为某些实数定义,称为其域,例如,平方根和对数函数只为非负数定义,因此如果我们在这些函数中传递负参数,它们 将厄尔诺设定为以东
- ERANGE–距离误差 :可由变量表示的值的范围是有限的。例如,诸如pow之类的数学函数可以很容易地输出由浮点变量表示的范围,或者诸如strtod之类的函数可以遇到比范围表示值更长的数字序列。 在这些情况下,errno设置为ERANGE。
- EILSEQ–非法序列 :多字节字符序列可能有一组有限的有效序列。当一组多字节字符被mbrtowc等函数翻译时, 遇到无效序列时,errno设置为EILSEQ。
以下是实施工作流程的程序 呃不 : 项目1: 该程序在日志函数中传递负值时检测错误。
CPP
#include <cerrno> #include <clocale> #include <cmath> #include <cstring> #include <iostream> using namespace std; int main() { // log function doesn't take negative value // thus it changes value of errno to some positive number double not_valid = log (-1.0); // check if value of errno same as value of EDOM i.e. 33 if ( errno == EDOM) { cout << " Value of errno is : " << errno << '' ; cout << " log(-1) is not valid : " << strerror ( errno ) << '' ; } return 0; } |
输出:
Value of errno is : 33 log(-1) is not valid : Numerical argument out of domain
项目2: 该程序在平方根函数中传递负值时检测错误。
CPP
#include <cerrno> #include <clocale> #include <cmath> #include <cstring> #include <iostream> using namespace std; int main() { // sqrt function doesn't take negative value // thus it changes value of errno to some positive number double not_valid = sqrt (-100); // check if value of errno same as value of EDOM i.e. 33 if ( errno == EDOM) { cout << " Value of errno is : " << errno << '' ; cout << " -100 is not valid argument for square" << " root function : " << strerror ( errno ) << '' ; } return 0; } |
输出:
Value of errno is : 33 -100 is not valid argument for square root function : Numerical argument out of domain
方案3: 此程序将errno设置为ERANGE。
CPP
#include <bits/stdc++.h> using namespace std; // Driver code int main() { double x; double res; x = 5.000000; res = log (x); if ( errno == ERANGE) { cout << "Log(" << x << ") is out of range" ; } else { cout << "Log(" << x << ") = " << res << endl; } x = 10.00000; res = log (x); if ( errno == ERANGE) { cout << "Log(" << x << ") is out of range" ; } else { cout << "Log(" << x << ") = " << res << endl; } x = 0.000000; res = log (x); if ( errno == ERANGE) { cout << "Log(" << x << ") is out of range" ; } else { cout << "Log(" << x << ") = " << res << endl; } return 0; } |
输出:
Log(5) = 1.60944Log(10) = 2.30259Log(0) is out of range
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END