Platform-independent way to get maximum C ++ floating point value

What is the best platform-independent way to get the maximum value that can be stored in a C ++ float ?

+7
c ++ floating-point
source share
5 answers
+24
source share
 std::numeric_limits<float>::max() 
+20
source share

std :: numeric_limits

 // numeric_limits example #include <iostream> #include <limits> using namespace std; int main () { cout << "Minimum value for float: " << numeric_limits<float>::min() << endl; cout << "Maximum value for float: " << numeric_limits<float>::max() << endl; cout << "Minimum value for double: " << numeric_limits<double>::min() << endl; cout << "Maximum value for double: " << numeric_limits<double>::max() << endl; return 0; } 
+9
source share

In C ++, you can use the std::numeric_limits class to get such information.

If has_infinity is true (which will be true for all platforms now), you can use infinitity to get a value that is greater than or equal to all other values โ€‹โ€‹(except NaN). Similarly, its negation will give negative infinity and will be less than or equal to all other values โ€‹โ€‹(except NaNs again).

If you want to get the final values, you can use min / max (which will be less than or equal to / greater than or equal to all other final values).

+1
source share
 #include <float.h> 

then use FLT_MAX

-one
source share

All Articles