简体   繁体   中英

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

最好的,独立于平台的方式来获取可以存储在C ++中的float中的最大值?

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

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

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).

#include <float.h>

然后使用FLT_MAX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM