C 庫宏 ERANGE表示一個範圍錯誤,它在輸入參數超出數學函數定義的範圍時發生,errno 被設置為 ERANGE.而errno是在程序執行過程中設置的保存錯誤代碼的全局宏,以及用於顯示的錯誤代碼的等效字符串。下面的實例演示了 ERANGE 宏的用法。
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <iostream>
using namespace std;
int main()
{
double x;
double value;
x = 5.000000;
value = log(x);
if (errno == ERANGE)
{
cout <<"Log("<<x<<") is out of range" << endl;
}
else
{
cout << "Log(" << x << ") ="<<value << endl;
}
x = 3.000000;
value = log(x);
if (errno == ERANGE)
{
cout << "Log(" << x << ") is out of range" << endl;
}
else
{
cout << "Log(" << x << ") =" << value << endl;
}
x = 0;
value = log(x);
if (errno == ERANGE)
{
cout << "Log(" << x << ") is out of range" << endl;
}
else
{
cout << "Log(" << x << ") =" << value << endl;
}
system("pause");
return 0;
}