views:

173

answers:

3

I was looking at std::numeric_limits<float>::min/max() but it appears 'min()' returns the smallest absolute value, not the lowest value. Is it safe to use

-std::numeric_limits<float>::max(), i.e is float symmetric in min/max limits?

+4  A: 

use std::numeric_limits::lowest()

static _Ty __CRTDECL lowest() _THROW0()
    {   // return most negative value
    return (-(max)());
    }
Chubsdad
http://www.cplusplus.com/reference/std/limits/numeric_limits/ - it doesn't mention `lowest`, is it standard?
John
@John: Yes on C++0x, no on C++98.
KennyTM
@KennyTM: Thanks. I was clueless on the C++0x part of that.
Chubsdad
I don't think I deserve a vote. Who's upvoting?
Chubsdad
@chusbad: I haven't, but your answer is (partly) correct since the version of the standard wasn't mentioned in the question :)
Matthieu M.
+1 for the info about std::numeric_limits::lowest()
Stephane Rolland
+3  A: 

IEEE 754 floating point numbers use a sign bit for signed-ness (rather than something like twos complement), so if you're sure that your compiler/platform uses that representation (very common) then you can use -std::numeric_limits<float>::max() as you suspected.

Mark B
+2  A: 

Yes, float is symmetric in minimum/maximum values.

If you're using the lowest representable value as an initial value in searching a list for its maximum value, consider using infinity instead.

std::numeric_limits<T>::has_infinity() will return true for any numeric type that has it and std::numeric_limits<T>::infinity() will return a value that always evaluates greater than any other non-NaN value for that type. This value can be negated and will evaluate less than anything else.

Blrfl