今天做BS的TC++PL第四章的习题,有一个求各种类型的最大最小值的题,要用到 numeric_limits<class T>模版类,做完题以后,查了一下该模版类的用法,在本文做个总结。
我的求各种类型范围的函数:
void range()
{
cout<<"largest int = = "<<numeric_limits<int>::max()<<endl;
cout<<"smallest int = = "<<numeric_limits<int>::min()<<endl;
cout<<"largest char = = "<<int(numeric_limits<char>::max())<<endl;
cout<<"smallest char = = "<<int(numeric_limits<char>::min())<<endl;
cout<<"largest short = = "<<numeric_limits<short>::max()<<endl;
cout<<"smallest short = = "<<numeric_limits<short>::min()<<endl;
cout<<"largest long = = "<<numeric_limits<long>::max()<<endl;
cout<<"smallest long = = "<<numeric_limits<long>::min()<<endl;
cout<<"largest float = = "<<numeric_limits<float>::max()<<endl;
cout<<"smallest float = = "<<numeric_limits<float>::min()<<endl;
cout<<"largest double = = "<<numeric_limits<double>::max()<<endl;
cout<<"smallest double = = "<<numeric_limits<double>::min()<<endl;
cout<<"largest long double = = "<<numeric_limits<long double>::max()<<endl;
cout<<"smallest long double = = "<<numeric_limits<long double>::min()<<endl;
cout<<"largest unsigned = = "<<numeric_limits<unsigned>::max()<<endl;
cout<<"smallest unsigned = = "<<numeric_limits<unsigned>::min()<<endl;
}
返回结果:
largest int = = 2147483647
smallest int = = -2147483648
largest char = = 127
smallest char = = -128
largest short = = 32767
smallest short = = -32768
largest long = = 2147483647
smallest long = = -2147483648
largest float = = 3.40282e+038
smallest float = = 1.17549e-038
largest double = = 1.79769e+308
smallest double = = 2.22507e-308
largest long double = = 1.79769e+308
smallest long double = = 2.22507e-308
largest unsigned = = 4294967295
smallest unsigned = = 0
个人觉得这个程序有两个问题:
第一:程序太罗嗦,重复太多,不简练,准备定义各种类型的一个枚举值,简化代码,明天来做;
第二:对程序的结果有疑问:smallest float = = 1.17549e-038 ,float的最小值怎么是个正值呢?和机器有关,还是跟这个函数有关?我的机器是inter P4,64位的。请大家指教!
附录1:
问:from Csdn,richion(阿琥)
numeric_limits<class T>是一个模板类啊
直接调用numeric_limits<float>::max()对吗?
类是一个抽象的东西,应该先实例化建立一个对象
我认为应该先定义一个对象,象这样:
numeric_limits<float> floLim;
cout<<"largest float="<<floLim:max()<<endl;
事实上两种写法都对
我很奇怪为什么可以象的一种那么写,高手指教
答: from Csdn ,whyglinux(山青水秀)
这是因为其中的 max() 以及 min() 等成员函数都是static函数,所以可以直接通过类名来访问它们。
附录2:
from Csdn,plusa
因为比较有用,所以自己试验并且翻译了一下,很可能有错误,希望发现错误的朋友能给我留言纠正,谢谢!
有部分没有翻译,因为还没弄清楚到底什么作用
numeric_limits是模板类。
需要注意的是返回值和参数,可以用强制转换。测试类的函数返回1代表“是”,0代表“否”。
例如cout << " The minimum value for char is " << (int)numeric_limits<char>::min() << endl;
(注释:所谓规范化表达形式,指小数点位于第一个不为0的数字后面)
digits10 |
返回目标类型在十进制下可以表示的最大位数 |
epsilon |
返回目标数据类型能表示的最逼近1的正数和1的差的绝对值 |
has_denorm |
测试目标类型是不是可以非规范化表示示 |
has_denorm_loss |
测试所有类型是不是能测出因为非规范化而造成的精度损失(不是因为结果本身的不精确) |
has_infinity |
测试目标类型是不是能表示无限(比如被0除,或者其他一些情况) |
has_quiet_NaN |
检查目标类型是不是支持安静类型的NaN |
has_signaling_NaN |
检查目标类型是不是支持信号类型的NaN |
infinity |
检查目标类型的无限类型(如果支持无限表示) |
is_bounded |
检查目标类型的取值是否有限 |
is_exact |
测试目标类型的计算结果是不是不会造成舍入误差(比如float是0) |
is_iec559 |
测试目标类型是不是符合IEC559标准 |
is_integer |
测试目标类型是不是可以用整型来表示(比如char是1,float是0) |
is_modulo |
Tests if a type has a modulo representation. |
is_signed |
测试目标类型是否是带符号的 |
is_specialized |
测试目标类型是不是在numeric_limits .模板类中有特殊定义 |
max |
返回可取的有限最大值 |
max_exponent |
Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power. |
max_exponent10 |
Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power. |
min |
返回可取的最小值(规范化) |
min_exponent |
Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power. |
min_exponent10 |
Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power. |
quiet_NaN |
返回目标类型的安静NAN的表示 |
radix |
Returns the integral base, referred to as radix, used for the representation of a type. |
round_error |
返回目标类型的最大可能的舍入误差 |
round_style |
Returns a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value. |
signaling_NaN |
返回目标类型关于信号NAN的表示 |
tinyness_before |
测试目标类型是不是能测定出微小的舍入误差 |
traps |
Tests whether trapping that reports on arithmetic exceptions is implemented for a type. |