Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain minimum NEGATIVE float value in C++

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?

like image 464
Mr. Boy Avatar asked Aug 20 '10 08:08

Mr. Boy


People also ask

Can float take negative values in C?

Floating point numbers can be positive or negative.

How do you find the minimum float value?

Wiki says this about float : The minimum positive normal value is 2^−126 ≈ 1.18 × 10^−38 and the minimum positive (denormal) value is 2^−149 ≈ 1.4 × 10^−45.

Can float hold negative value?

The range of float values is 3.4e-38 to 3.4e+38. So the float variables should not store negative values.


1 Answers

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.

like image 146
Mark B Avatar answered Sep 21 '22 13:09

Mark B