Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::numeric_limits — numeric type limits and properties

std::numeric_limits<T> from <limits> exposes the limits and representation properties of T. Learn how min() differs from lowest(), what digits measures, and how to check IEC 559 conformance without assuming a particular type size.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <limits>
std::numeric_limits<T>::min()
std::numeric_limits<T>::max()
std::numeric_limits<T>::digits
std::numeric_limits<T>::is_iec559

std::numeric_limits<T> from <limits> describes an implementation's representation of an arithmetic type T. Its static constexpr members can be used without constructing an object, including in static_assert. In generic code, check is_specialized before relying on properties of an unfamiliar type.

std::numeric_limits<T>::max() returns the largest finite value, not infinity. For integer types, std::numeric_limits<T>::min() is the lower bound; for floating-point types, it is the smallest positive normalized value. lowest() returns the lowest finite value, not negative infinity, even if the type supports negative infinity. denorm_min() returns the smallest positive subnormal value if available; otherwise, it returns the smallest positive normalized value.

std::numeric_limits<T>::digits counts digits in base radix: value bits excluding the sign bit for integers, and significand digits in that base for floating-point types. It is not generally a decimal digit count; digits10 describes that property.

std::numeric_limits<T>::is_iec559 is a Boolean constant reporting whether the type conforms to ISO/IEC 60559 (IEEE 754). Do not assume it is always true for float, or that digits has a particular value. The example uses guaranteed bool properties and comparisons for float, so it does not depend on common representation sizes.

Reference identifiers covered by this entry: std::numeric_limits::is_iec559, std::numeric_limits::digits, std::numeric_limits::max, std::numeric_limits::min.

Examples

#include <iostream>
#include <limits>

int main() {
    using B = std::numeric_limits<bool>;
    using F = std::numeric_limits<float>;

    static_assert(B::is_specialized);
    static_assert(B::digits == 1);
    static_assert(!B::is_iec559);

    std::cout << std::boolalpha;
    std::cout << "bool range: " << B::min() << ' ' << B::max() << '\n';
    std::cout << "bool digits: " << B::digits << '\n';
    std::cout << "bool IEC 559: " << B::is_iec559 << '\n';
    std::cout << "float min positive: " << (F::min() > 0.0f) << '\n';
    std::cout << "float lowest negative: " << (F::lowest() < 0.0f) << '\n';
}

Result

bool range: false true
bool digits: 1
bool IEC 559: false
float min positive: true
float lowest negative: true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources