Namespaces
Variants

std:: fpclassify

From ja.cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
fpclassify
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
ヘッダーで定義 <cmath>
(1)
int fpclassify ( float num ) ;

int fpclassify ( double num ) ;

int fpclassify ( long double num ) ;
(C++11以降)
(C++23まで)
constexpr int fpclassify ( /* floating-point-type */ num ) ;
(C++23以降)
ヘッダーで定義 <cmath>
template < class Integer >
int fpclassify ( Integer num ) ;
(A) (C++11以降)
(C++23以降constexpr)
1) 浮動小数点値 num を以下のカテゴリに分類する: ゼロ、非正規化数、正規化数、無限大、NaN、または実装定義のカテゴリ。 ライブラリは、 std::fpclassify のオーバーロードを、パラメータ num の型としてすべてのcv修飾されていない浮動小数点型に対して提供する。 (C++23以降)
A) すべての整数型に対して追加のオーバーロードが提供されており、これらは double として扱われます。

目次

パラメータ

num - 浮動小数点または整数値

戻り値

FP_INFINITE 、 FP_NAN 、 FP_NORMAL 、 FP_SUBNORMAL 、 FP_ZERO のいずれか、または実装定義の型で、 num のカテゴリを指定します。

注記

追加のオーバーロードは (A) と完全に同一である必要はありません。それらは、整数型の引数 num に対して、 std :: fpclassify ( num ) が std :: fpclassify ( static_cast < double > ( num ) ) と同じ効果を持つことを保証するのに十分であればよいのです。

例

#include <cfloat>
#include <cmath>
#include <iostream>
auto show_classification(double x)
{
    switch (std::fpclassify(x))
    {
        case FP_INFINITE:
            return "Inf";
        case FP_NAN:
            return "NaN";
        case FP_NORMAL:
            return "normal";
        case FP_SUBNORMAL:
            return "subnormal";
        case FP_ZERO:
            return "zero";
        default:
            return "unknown";
    }
}
int main()
{
    std::cout << "1.0/0.0 is " << show_classification(1 / 0.0) << '\n'
              << "0.0/0.0 is " << show_classification(0.0 / 0.0) << '\n'
              << "DBL_MIN/2 is " << show_classification(DBL_MIN / 2) << '\n'
              << "-0.0 is " << show_classification(-0.0) << '\n'
              << "1.0 is " << show_classification(1.0) << '\n';
}

出力:

1.0/0.0 is Inf
0.0/0.0 is NaN
DBL_MIN/2 is subnormal
-0.0 is zero
1.0 is normal

関連項目

(C++11)
指定された数値が有限値を持つかどうかをチェックする
(関数)
(C++11)
指定された数値が無限大かどうかをチェックする
(関数)
(C++11)
指定された数値がNaNかどうかをチェックする
(関数)
(C++11)
指定された数値が正規化数かどうかをチェックする
(関数)
すべての基本数値型のプロパティを問い合わせるためのインターフェースを提供する
(クラステンプレート)
Cドキュメント for fpclassify