Basically i want to search for the smallest (positive) value from a bunch of values and need a default value to compare to the first one. The naïve assumption would be, they always compare “less than” (except NaNs, but let’s not consider those) but I’m not quite sure.
I’m using the float type and I think it can safely be assumed that my target hardware implements a infinity value.
Here’s some example code:
auto leastValue = std::numeric_limits<float>::infinity();
for (auto i = something.begin(), e = something.end(); i != e; ++i)
{
auto value = (*i)->GetValue();
if (value < leastValue)
{
leastValue = value;
}
}
For IEEE 754 floats, except NaN and infinity, everything is less than infinity. Chances are you’ll have those on your platform. If you’re paranoid, check with
numeric_limits<float>::is_iec559. If your platform happens to be not conforming, usenumeric_limits<float>::max(); if your sequence is nonempty, it will not give you a wrong result.