📜  C / C++中的fpclassify()方法与示例

📅  最后修改于: 2021-05-30 18:26:48             🧑  作者: Mango

fpclassify()函数在C的标头math.h标头C++的cmath库中定义。此函数用于获取与分类宏常量之一(取决于x的值)匹配的int类型的值。

句法:

int fpclassify(int x);
int fpclassify(float x);
int fpclassify(double x);
int fpclassify(long double x);

参数:此方法接受参数x ,该参数x是与该方法的宏常量之一匹配的值。它可以是整数,浮点数,双精度型或长双精度型。

返回值:该函数返回宏常量的整数值,如下所示:

  • FP_INFINITE :当指定的值是正无穷大或负无穷大时
  • FP_NAN :当指定的值不是数字时
  • FP_ZERO :当指定值为零时。
  • FP_SUBNORMAL :当指定值是正或负非正规化值时
  • FP_NORMAL :当指定值是正或负归一化非零值时

下面的示例演示fpclassify()方法的用法:

// C++ program to demonstrate
// the use of fpclassify() method
  
#include 
#include 
using namespace std;
  
// Function to implement fpclassify() method
void fpclassification(double x)
{
  
    // fpclassify() method
    switch (fpclassify(x)) {
  
    // For the data to be infinite
    case FP_INFINITE:
        cout << "Infinite Number \n";
        break;
  
    // For the data to be not defined
    // as in divide by zero
    case FP_NAN:
        cout << "Not a Number \n";
        break;
  
    // For the data to be zero
    case FP_ZERO:
        cout << "Zero \n";
        break;
  
    // For the data to be subnormal
    case FP_SUBNORMAL:
        cout << "Subnormal value \n";
        break;
  
    // For the data to be normal
    case FP_NORMAL:
        cout << "Normal value \n";
        break;
  
    // For the data to be invalid
    default:
        cout << "Invalid number \n";
    }
}
  
// Driver code
int main()
{
  
    // Example 1
    double a = 1.0 / 0.0;
    cout << "For 1.0/0.0: ";
    fpclassification(a);
  
    // Example 2
    double b = 0.0 / 0.0;
    cout << "For 0.0/0.0: ";
    fpclassification(b);
  
    // Example 3
    double c = -0.0;
    cout << "For -0.0: ";
    fpclassification(c);
  
    // Example 4
    double d = 1.0;
    cout << "For 1.0: ";
    fpclassification(d);
  
    return 0;
}
输出:
For 1.0/0.0: Infinite Number 
For 0.0/0.0: Not a Number 
For -0.0: Zero 
For 1.0: Normal value
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”