3 数值的极限(Numeric Limit)
- 数值类型一般有与平台相互依靠的极值;
- c++使用template numeric_limits提供,定义于头文件
- c语言采用的是预处理常量:整数定义在
<climits>和<limits.h>
,浮点数定义在<cfloat>和<float.h>
c++定义的各类型的最小精度:
1 class numeric_limits<>
1.1 numeric_limits<>的所有成员
例子:
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tlowest\thighest\n";
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
结果为:
type lowest highest
int -2147483648 2147483647
float -3.40282e+38 3.40282e+38
double -1.79769e+308 1.79769e+308