Max, min and lowest in C++

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.

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. Finally, min for floating point types is the tiniest positive value that can be represented.

It is easier to understand by looking at the actual values for these types:

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.