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 | Return | Description |
|---|---|---|
| is_specialized | bool | |
| radix | int | base of exponent |
| digits | int | number of radix digits in mantissa |
| digits10 | int | number of base 10 digits in mantissa |
| is_signed | bool | |
| is_integer | bool | |
| is_exact | bool | |
| 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_exponent | int | |
| min_exponent10 | int | |
| max_exponent | int | |
| max_exponent10 | int | |
| has_infinity | bool | |
| has_quiet_NaN | bool | |
| has_signaling_NaN | bool | |
| has_denorm | <type>_denorm_style | |
| has_denorm_loss | bool | |
| is_iec559 | bool | conforms to IEC-559 |
| is_bounded | bool | |
| is_modulo | bool | |
| traps | bool | |
| tinyness_before | bool | |
| round_style | float_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;