Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::numeric_limits class template

Reference for std::numeric_limits: find min, max, lowest, epsilon, digits and infinity of arithmetic types, and avoid the min versus lowest trap.

std::numeric_limits<T> (header <limits>) describes the properties of an arithmetic type: its smallest and largest values, how many digits it holds, whether it is signed, whether it can represent infinity or NaN, and the precision of floating-point numbers. It is a class template with static members, specialized for every built-in arithmetic type (user-defined types are not specialized by default).

It replaces the old C macros INT_MAX, DBL_EPSILON and friends with something that works in templates: std::numeric_limits<T>::max() is correct for any T.

C++
int big = std::numeric_limits<int>::max();
double eps = std::numeric_limits<double>::epsilon();

Try it

C++ Limits of integer and floating types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Infinity, NaN and safe use

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class T> class numeric_limits;

static constexpr T min() noexcept;      // smallest finite value (smallest positive normal for floats)
static constexpr T max() noexcept;      // largest finite value
static constexpr T lowest() noexcept;   // most negative finite value (C++11)
static constexpr T epsilon() noexcept;  // floats: gap between 1 and the next value
static constexpr T infinity() noexcept;
static constexpr T quiet_NaN() noexcept;
static constexpr int digits;            // radix digits in the mantissa (value bits for integers)
static constexpr int digits10;          // decimal digits that round-trip
static constexpr int max_digits10;      // digits needed to print a float uniquely (C++11)
static constexpr bool is_signed, is_integer, is_exact, is_iec559, has_infinity, has_quiet_NaN, is_bounded, is_modulo;
Member Description
min() Smallest finite value for integers; for floating types the smallest positive normalized value.
max() Largest finite value.
lowest() Smallest (most negative) finite value; equals min() for integers.
epsilon() Difference between 1.0 and the next representable value.
round_error(), denorm_min() Maximum rounding error and the smallest positive subnormal.
infinity(), quiet_NaN(), signaling_NaN() Special values; check has_infinity and friends first.
digits, digits10, max_digits10 Precision information.
is_specialized true for types with a specialization.

Returns: values of type T (or int / bool for the constants). All are constexpr.

Errors / undefined behavior: none for the queries. For a type without a specialization, min(), max() and the others return T() and the flags are false.

Usage notes

  • min() is not the lowest value for floating-point types. numeric_limits<double>::min() is about 2.2e-308, a tiny positive number. Use lowest() as the starting value of a "find the maximum" loop.
  • epsilon() is relative to 1. For numbers of other magnitudes scale it, for example eps * std::max(std::abs(a), std::abs(b)), and pick a tolerance that suits the problem.
  • Signed overflow is undefined behavior. Check against max() or min() before adding or multiplying, as in the example, or use unsigned arithmetic (which wraps) deliberately.
  • digits for signed integers excludes the sign bit. int8_t has 7, unsigned char has 8.
  • max_digits10 is the number of significant digits you need to print to read back the exact same float: use std::setprecision(std::numeric_limits<double>::max_digits10).
  • char signedness is implementation-defined. numeric_limits<char>::is_signed tells you which it is on this platform.
  • Everything is constexpr, so the values work in static_assert, array sizes and template arguments.
  • C macros from <climits>, <cfloat> and <cstdint> (INT_MAX, DBL_EPSILON, UINT64_MAX) still exist and have the same values; prefer the template in generic code.
  • Parentheses around max ((std::numeric_limits<int>::max)()) are needed in Windows code that includes <windows.h> without NOMINMAX.