example4_precision_float_types.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
27
28
#include <iostream>
#include <limits>
#include <iomanip>

template<typename T>
void print_float_info() {
    std::cout << "Type: " << typeid(T).name() << std::endl;
    std::cout << "Digits10: " << std::numeric_limits<T>::digits10 << std::endl;
    std::cout << "Max exponent: " << std::numeric_limits<T>::max_exponent10 << std::endl;
    std::cout << "Epsilon: " << std::numeric_limits<T>::epsilon() << std::endl;
    std::cout << "Round error: " << std::numeric_limits<T>::round_error() << std::endl;
    std::cout << std::endl;
}

int main() {
    std::cout << std::setprecision(std::numeric_limits<long double>::digits10 + 1);

    std::cout << "Float info:" << std::endl;
    print_float_info<float>();

    std::cout << "Double info:" << std::endl;
    print_float_info<double>();

    std::cout << "Long double info:" << std::endl;
    print_float_info<long double>();

    return 0;
}
Back to limits