c++的float类型包含的最值问题....

来源:互联网 发布:idc数据统计 编辑:程序博客网 时间:2024/05/16 11:27

在各种纠结的算法中。偶尔出现怎么声明一个无穷大的float宏.....

 方法一:

   头文件:#include<floati.h>或#include<cfloat>

   宏: FLT_MAX

   最大值:3.402823466e+38F


方法二:

   头文件:#include <limits>

   定义方式:float floatMax = numeric_limits<float>::max();

   如下例:(借用网友的.....)

[html] view plaincopy
  1. #include <iostream>  
  2. #include <limits>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.  int intMax = numeric_limits<int>::max();  
  9.  int intMin = numeric_limits<int>::min();  
  10.   
  11.  float floatMax = numeric_limits<float>::max();  
  12.  float floatMin = numeric_limits<float>::min();  
  13.   
  14.  cout << intMax << " " << intMin << endl;  
  15.  cout << floatMax << " " << floatMin << endl;  
  16.   
  17.   return 0;  
  18. }  

运行结果如下:


0 0
原创粉丝点击