當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ fpclassify()用法及代碼示例


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 <iostream> 
#include <math.h> 
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




相關用法


注:本文由純淨天空篩選整理自verma_anushka大神的英文原創作品 fpclassify() method in C/C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。