Max, min and lowest in C++

The limits header provides the template methods that can be called to obtain the maximum and minimum values of any numeric type in C++. For example, max() returns the maximum value. For int, this is equivalent to obtaining the value of INT_MAX in C.

Similarly, min() returns the minimum value. Note that for float and double, this returns the smallest positive value for that type. FLT_MIN and DBL_MIN return the same in C. This is necessary in many programs where you might want to know the smallest granularity of the float or double type.

However, in most applications using float or double, you actually want to know the lowest value, such that there is no other value in this type that is lesser. This is typically needed to assign a starting value when looking for the biggest value in some sequence.

Thankfully, C++11 now has lowest() that can be used for this. This is equivalent to -FLT_MAX and -DBL_MAX.

Tried with: GCC 4.9.2 and Ubuntu 14.04


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s