Tony the Pony
Tony the Pony

Reputation: 41357

Platform-independent way to obtain maximum C++ float value

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

Answers (5)

Simon Byrne
Simon Byrne

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

Georg Fritzsche
Georg Fritzsche

Reputation: 98984

std::numeric_limits    

Upvotes: 25

hrnt
hrnt

Reputation: 10142

std::numeric_limits<float>::max()

Upvotes: 21

tster
tster

Reputation: 18237

#include <float.h>

then use FLT_MAX

Upvotes: -1

Todd
Todd

Reputation: 2401

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;
}

Upvotes: 9

Related Questions