numeric_limits

This templated class provides various information about the built-in types.

Limits for numeric values are defined in the <limits> header. The templated values of numeric_limits provide system-dependent numerical representations of the C++ data types. Use the appropriate function given the data type as the template argument as shown in the table below. Note that numeric_limits can be overloaded for user-defined types as well.

Method or
constant
ReturnDescription
is_specializedbool
radixintbase of exponent
digitsintnumber of radix digits in mantissa
digits10intnumber of base 10 digits in mantissa
is_signedbool
is_integerbool
is_exactbool
min()<type>smallest number that can be respresented (not the most negative)
max()<type>largest number
epsilon()<type>inherent representation error value, so that 1 + epsilon > 1
round_error()<type>maximum rounding adjustment possible
infinity()<type>
quiet_NaN()<type>invalid number that does not signal floating point error
signaling_NaN()<type>invalid number that signals floating point error
denorm_min()<type>
min_exponentint
min_exponent10int
max_exponentint
max_exponent10int
has_infinitybool
has_quiet_NaNbool
has_signaling_NaNbool
has_denorm<type>_denorm_style
has_denorm_lossbool
is_iec559boolconforms to IEC-559
is_boundedbool
is_modulobool
trapsbool
tinyness_beforebool
round_stylefloat_round_style { round_to_nearest, … }

The most common usage is in bounds checking, to determine the minimum and maximum values a data type can hold. The following code prints out the minimum and maximum values for a short on the system it is run.

  #include <limits>
  std::cout << "Maximum short value: " << std::numeric_limits<short>::max() << std::endl;
  std::cout << "Minimum short value: " << std::numeric_limits<short>::min() << std::endl;