Blog

std::numeric_limits::min() vs. std::numeric_limits::lowest()

While this is quite a technical blog post it took me a little while to fully understand the implications on my code and I therefore would like to share this insight with my readers. This article applies to everyone who likes to use software constructs like the following one:

Looks reasonable, doesn't it? However, the tricky bit is that min() does return the minimum representable finite POSITVE value of the type provided as template argument (in this case float). If x and some_other_value are always positive, this is not a problem. If some_other_value can also be negative, above statement will not work as intented.

What is the correct solution? Use std::numeric_limtis<T>::lowest() (Only available from C++11 on!). If I print the concrete values of min(), max() and lowest() on my dev system I do get the following results:

The correct code sequence should therefore be:

This stumbling block is especially heinous because the following software construct works as desired in every case:

0
  Related Posts
  • No related posts found.