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


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

該函數返回與宏常量之一匹配的 int 類型值,具體取決於 x 的值。

描述
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 <iostream>
#include<math.h>
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 是一個無限數。






相關用法


注:本文由純淨天空篩選整理自 C++ Math fpclassify()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。