原文地址:http://hi.baidu.com/qingmuxiaoyao/blog/item/570184fd6daff840d7887de5.html
在c++的<limits>中定义了如下的一个模板类:
template<class Type> class numeric_limits
它可以求出下列内置类型的一些特性: wchar_t, bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double, and long double.
有这么四个函数,比较有用: max() Returns the maximum finite value for a type. min() Returns the minimum normalized value for a type.
digits() Returns the number of radix digits that the type can represent without loss of precision. digits10() Returns the number of decimal digits that the type can represent without loss of precision.
max()举例如下:msdn2005上的例子
// numeric_limits_max.cpp #include <iostream> #include <limits>
using namespace std;
int main() { cout << "The maximum value for type float is: " << numeric_limits<float>::max( ) << endl; cout << "The maximum value for type double is: " << numeric_limits<double>::max( ) << endl; cout << "The maximum value for type int is: " << numeric_limits<int>::max( ) << endl; cout << "The maximum value for type short int is: " << numeric_limits<short int>::max( ) << endl; }
Output
The maximum value for type float is: 3.40282e+038 The maximum value for type double is: 1.79769e+308 The maximum value for type int is: 2147483647 The maximum value for type short int is: 32767
digits()举例: // numeric_limits_digits_min.cpp #include <iostream> #include <limits>
using namespace std;
int main( ) { cout << numeric_limits<float>::digits <<endl; cout << numeric_limits<double>::digits <<endl; cout << numeric_limits<long double>::digits <<endl; cout << numeric_limits<int>::digits <<endl; cout << numeric_limits<__int64>::digits <<endl; }
Output
24 53 53 31 63
digits10举例: // numeric_limits_digits10.cpp #include <iostream> #include <limits>
using namespace std;
int main( ) { cout << numeric_limits<float>::digits10 <<endl; cout << numeric_limits<double>::digits10 <<endl; cout << numeric_limits<long double>::digits10 <<endl; cout << numeric_limits<int>::digits10 <<endl; cout << numeric_limits<__int64>::digits10 <<endl; }
Output
6 15 15 9 18
==============
其实,得到最大无符号整型数有个简便方法:
{
unsigned int a=-1;
cout<<a<<endl;
}
|
请发表评论