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.)
FLT_MAX is the largest representable real value. – Paul R. Nov 1, 2011 at 22:31. Great question. Of course, in C++ you can (and should) use numeric_limits<float>::min() , numeric_limits<float>::max() , and numeric_limits<float>::infinity() ... But I always wondered the same thing myself.
FLT_MIN refers to the minimum normalized positive float. The minimum positive float is a denormalized number, namely 2^(-149) ≈ 1.4013e-45.
FLT_MAX is the maximum value of a float.
The purpose of FLT_MIN
/MAX
is to tell you what the smallest and largest representable floating-point numbers are. Infinity isn't a number; it's a limit.
what use could FLT_MAX or FLT_MIN have as they currently lie in the middle of the representable numeric range of float?
They do not lie in the middle of the representable range. There is no positive float value x
which you can add to FLT_MAX
and get a representable number. You will get +INF. Which, as previously stated, is not a number.
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.)
And how doesn't it "work fine?" It gives you the smallest value. The only situation where it doesn't "work fine" is if the table contains only +INF. And even in that case, it returns an actual number, not an error-code. Which is probably the better option anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With