example2_special_values.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <limits>
#include <cmath>

int main() {
    // Infinity
    if (std::numeric_limits<float>::has_infinity) {
        float pos_inf = std::numeric_limits<float>::infinity();
        float neg_inf = -std::numeric_limits<float>::infinity();
        std::cout << "Positive infinity: " << pos_inf << std::endl;
        std::cout << "Negative infinity: " << neg_inf << std::endl;
    }

    // NaN (Not a Number)
    if (std::numeric_limits<double>::has_quiet_NaN) {
        double nan = std::numeric_limits<double>::quiet_NaN();
        std::cout << "NaN: " << nan << std::endl;
        std::cout << "Is NaN: " << std::isnan(nan) << std::endl;
    }

    // Checking if a type is integer
    std::cout << "Is int integer: " << std::numeric_limits<int>::is_integer << std::endl;
    std::cout << "Is float integer: " << std::numeric_limits<float>::is_integer << std::endl;

    return 0;
}
Back to limits