📅 2015-Jul-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, limits, lowest, max, min, numeric_limits ⬩ 📚 Archive
The limits
header provides template methods to obtain the maximum, minimum and lowest values of any numeric type in C++. The names of these methods and a slight distinction in these methods between integral types and floating point types causes a bit of confusion.
#include <limits>
std::numeric_limits<int>::max() // INT_MAX
std::numeric_limits<int>::min() // INT_MIN
std::numeric_limits<int>::lowest() // INT_MIN
std::numeric_limits<float>::max() // FLT_MAX
std::numeric_limits<float>::min() // FLT_MIN
std::numeric_limits<float>::lowest() // -FLT_MAX
std::numeric_limits<double>::max() // DBL_MAX
std::numeric_limits<double>::min() // DBL_MIN
std::numeric_limits<double>::lowest() // -DBL_MAX
For integral types, max
is the largest integral value and min
is the least representable integral value for that type. That is, all the integral values range from min
to max
. lowest
is the same value as min
for integral types.
For floating point types, max
is the largest value that can be represented. That is, no other floating point value lies to the right of this value on the number line. lowest
is the least value, no other value lies to the left of this value on the number line. Thus, all the floating point values range from lowest
to max
.
min
is where floating point types differ from integral types. For floating point types, min
is the tiniest positive value that can be represented. If min
of integral values had been defined similar to floating point types, then that value would have been 1
.
I maintain this table for easy lookup of these values:
2147483647 // int max
-2147483648 // int min
-2147483648 // int lowest
3.40282e+38 // float max
1.17549e-38 // float min
-3.40282e+38 // float lowest
1.79769e+308 // double max
2.22507e-308 // double min
-1.79769e+308 // double lowest
Tried with: GCC 4.9.2 and Ubuntu 14.04