C++ Math fpclassify()函数
该函数根据x的值返回与宏常量之一匹配的int类型的值。
| 值 | 描述 | 
|---|---|
| FP_INFINITE | 正无穷或负无穷 | 
| FP_NAN | 非数字 | 
| FP_ZERO | 零值 | 
| FP_SUBNORMAL | 亚正常值 | 
| FP_NORMAL | 正常值 | 
语法
假设一个数字为x。语法如下:
int fpclassify(float x);
int fpclassify(double x);
int fpclassify(long double x);
int fpclassify(int x);
参数
x : 要与宏常量之一进行匹配的值。
返回值
返回以下整数值:FP_INFINITE,FP_NAN,FP_ZERO,FP_SUBNORMAL,FP_NORMAL。
示例
让我们看一个简单的示例。
#include 
#include
using namespace std;
int main()
{
    double d=1.0/0.0;
    switch(fpclassify(d))
    {
    case FP_INFINITE:
    cout<<"1.0/0.0 is a infinite number ";  
    break;
    case FP_NAN:
    cout<<"1.0/0.0 is Not a Number";
    break;
    case FP_ZERO:
    cout<<"1.0/0.0 is zero.";
    break;
    case FP_SUBNORMAL:
    cout<<"1.0/0.0 is a subnormal value";
    break;
    case FP_NORMAL:
    cout<<"1.0/0.0 is a normal value";
    break;
    default:
    cout<<"wrong number";
    }
    return 0;
}
输出:
1.0/0.0 is a infinite number
在这个示例中,fpclassify()函数确定x是一个无穷大的数。
 极客笔记
极客笔记