isinf を使ってみる (C++)
C++
Published: 2020-05-17

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

数値が無限大(infinity)であるか判定する。

test.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    double tmp = 1e100000 / 1e-10;

    cout << INFINITY << endl;
    cout << std::numeric_limits<float>::infinity() << endl;
    cout << tmp << endl;

    cout << "-- inf --" << endl;
    cout << isinf(INFINITY) << endl;
    cout << isinf(std::numeric_limits<float>::infinity()) << endl;
    cout << isinf(tmp) << endl;

    cout << "-- not inf --" << endl;
    cout << isinf(0) << endl;
    cout << isinf(100) << endl;
}

出力結果

inf
inf
inf
-- inf --
1
1
1
-- not inf --
0
0

参考