Logically speaking, given the nature of floating point values, the maximum and minimum representable values of a float
are positive and negative infinity, respectively.
Why, then, are FLT_MAX
and FLT_MIN
not set to them? I understand that this is "just how the standard called for". But then, what use could FLT_MAX
or FLT_MIN
have as they currently lie in the middle of the representable numeric range of float
? Other numeric limits have some utility because they make guarantees about comparisons (e.g. "No INT can test greater than INT_MAX"). Without that kind of guarantee, what use are these float limits at all?
A motivating example for C++:
#include <vector>
#include <limits>
template<typename T>
T find_min(const std::vector<T> &vec)
{
T result = std::numeric_limits<T>::max();
for (std::vector<T>::const_iterator p = vec.start() ; p != vec.end() ; ++p)
if (*p < result) result = *p;
return result;
}
This code works fine if T is an integral type, but not if it is a floating point type. This is annoying. (Yes yes, the standard library provides min_element
, but that is not the point. The point is the pattern.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…