Reputation: 41357
What’s the best, platform-independent way to obtain the maximum value that can be stored in a float
in C++?
Upvotes: 8
Views: 3906
Reputation: 7864
In C++ you can use the std::numeric_limits
class to get this sort of information.
If has_infinity
is true
(which will be true for basically all platforms nowadays), then you can use infinitity
to get the value which is greater than or equal to all other values (except NaNs). Similarly, its negation will give a negative infinity, and be less than or equal to all other values (except NaNs again).
If you want finite values, then you can use min
/max
(which will be less than or equal to/greater than or equal to all other finite values).
Upvotes: 1
Reputation: 2401
// 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;
}
Upvotes: 9