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:

1 2 |
float x = std::numeric_limit<float>::min(); x = std::max(x, some_other_value); |

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:

1 2 3 |
std::numeric_limits<float>::min() = 1.17549e-38 std::numeric_limits<float>::max() = 3.40282e+38 std::numeric_limits<float>::lowest() = -3.40282e+38 |

The correct code sequence should therefore be:

1 2 |
float x = std::numeric_limit<float>::lowest(); x = std::max(x, some_other_value); |

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

1 2 |
float x = std::numeric_limit<float>::max(); x = std::min(x, some_other_value); |

MRZ

2017