template< class T > class numeric_limits; |
|
|
| | |
The numeric_limits
class template provides a standardized way to query various properties of arithmetic types (e.g. the largest possible value for type int is std::numeric_limits<int>::max()).
This information is provided via specializations of the numeric_limits
template. The standard library makes available specializations for all arithmetic types:
template<> class numeric_limits<bool>;
template<> class numeric_limits<char>;
template<> class numeric_limits<signed char>;
template<> class numeric_limits<unsigned char>;
template<> class numeric_limits<wchar_t>;
template<> class numeric_limits<char16_t>; // C++11 feature
template<> class numeric_limits<char32_t>; // C++11 feature
template<> class numeric_limits<short>;
template<> class numeric_limits<unsigned short>;
template<> class numeric_limits<int>;
template<> class numeric_limits<unsigned int>;
template<> class numeric_limits<long>;
template<> class numeric_limits<unsigned long>;
template<> class numeric_limits<long long>;
template<> class numeric_limits<unsigned long long>;
template<> class numeric_limits<float>;
template<> class numeric_limits<double>;
template<> class numeric_limits<long double>; |
|
|
| | |
Additionally, a specialization exists for every cv-qualified version of each arithmetic type, identical to the unqualified specialization, e.g. std::numeric_limits<const int>
, std::numeric_limits<volatile int>
, and std::numeric_limits<const volatile int>
are provided and are equivalent to std::numeric_limits<int>
.
The standard library types that are aliases of arithmetic types (such as std::size_t or std::streamsize) may also be examined with the std::numeric_limits type traits.
Non-arithmetic standard types, such as std::complex<T> or std::nullptr_t, do not have specializations.
Implementations may provide specializations of std::numeric_limits for implementation-specific types: e.g. GCC provides std::numeric_limits<__int128>. Non-standard libraries may add specializations for library-provided types, e.g. OpenEXR provides std::numeric_limits<half> for a 16-bit floating-point type.
Template parameters
T
|
-
|
a type to retrieve numeric properties for
|
Member constants
|
identifies types for which std::numeric_limits is specialized (public static member constant) |
|
identifies signed types (public static member constant) |
|
identifies integer types (public static member constant) |
|
identifies exact types (public static member constant) |
|
identifies floating-point types that can represent the special value "positive infinity" (public static member constant) |
|
identifies floating-point types that can represent the special value "quiet not-a-number" (NaN) (public static member constant) |
|
identifies floating-point types that can represent the special value "signaling not-a-number" (NaN) (public static member constant) |
|
identifies the denormalization style used by the floating-point type (public static member constant) |
|
identifies the floating-point types that detect loss of precision as denormalization loss rather than inexact result (public static member constant) |
|
identifies the rounding style used by the type (public static member constant) |
|
identifies the IEC 559/IEEE 754 floating-point types (public static member constant) |
|
identifies types that represent a finite set of values (public static member constant) |
|
identifies types that handle overflows with modulo arithmetic (public static member constant) |
|
number of radix digits that can be represented without change (public static member constant) |
|
number of decimal digits that can be represented without change (public static member constant) |
|
number of decimal digits necessary to differentiate all values of this type (public static member constant) |
|
the radix or integer base used by the representation of the given type (public static member constant) |
|
one more than the smallest negative power of the radix that is a valid normalized floating-point value (public static member constant) |
|
the smallest negative power of ten that is a valid normalized floating-point value (public static member constant) |
|
one more than the largest integer power of the radix that is a valid finite floating-point value (public static member constant) |
|
the largest integer power of 10 that is a valid finite floating-point value (public static member constant) |
|
identifies types which can cause arithmetic operations to trap (public static member constant) |
|
identifies floating-point types that detect tinyness before rounding (public static member constant) |
Member functions
|
returns the smallest finite value of the given type (public static member function) |
|
returns the lowest finite value of the given type (public static member function) |
|
returns the largest finite value of the given type (public static member function) |
|
returns the difference between 1.0 and the next representable value of the given floating-point type (public static member function) |
|
returns the maximum rounding error of the given floating-point type (public static member function) |
|
returns the positive infinity value of the given floating-point type (public static member function) |
|
returns a quiet NaN value of the given floating-point type (public static member function) |
|
returns a signaling NaN value of the given floating-point type (public static member function) |
|
returns the smallest positive subnormal value of the given floating-point type (public static member function) |
Helper classes
|
indicates floating-point rounding modes (enum) |
|
indicates floating-point denormalization modes (enum) |
Relationship with C library macro constants
Example
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";
std::cout << "uchar\t"
<< +std::numeric_limits<unsigned char>::lowest() << '\t' << '\t'
<< +std::numeric_limits<unsigned char>::min() << '\t' << '\t'
<< +std::numeric_limits<unsigned char>::max() << '\n';
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::min() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::min() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::min() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
Possible output:
type lowest() min() max()
uchar 0 0 255
int -2147483648 -2147483648 2147483647
float -3.40282e+38 1.17549e-38 3.40282e+38
double -1.79769e+308 2.22507e-308 1.79769e+308
See also