I believe that's safe; all floating point formats in
use on any machine I've seen have the same range for
positive and negative numbers. (I'm not sure if the
C or C++ standards explicitly guarantee it, but in
this case that's fairly academic). This is maybe why
numeric_limits<X>::min() doesn't give a very negative
value for floating point types: the construction you
show above gives the real minimum value. (It's a shame
that min() does two different things depending on
whether the type is fp or integral, but that's another
issue.)
-- James
Well, a question to pose that might illustrate the reason min does
what it does.
What else would you call the function that gets the smallest positive
number representable as a float?
std::get_really_really_small_float() ?
I know thats awfully sarcastic, but really, although floats and ints
are theoretically the same, they are two fundamentally different data
types and represent two different things. If we think about the bit
representation it even makes a little sense on why this is the
behavior.
I don't know if floating point formats are included in the actual C++
standard or not, but generally their cited as being determined by IEEE
standards. My guess is that C++ doesn't specify this and leaves it as
an implementation detail, but I could see it going either way.
Oh and,
#include <iostream>
#include <limits>
int
main( int argc, char* argv[] )
{
std::cout << -std::numeric_limits<float>::max() << std::endl ;
}
compiles and spits out a really really big negative number. So yeah.
Its safe.
HTH,
Paul Davis