numeric_limits类及其用于判断类型的取值范围

来源:互联网 发布:网络小精灵粤语 编辑:程序博客网 时间:2024/06/05 11:43

numeric类的定义:

template<class T>    class numeric_limits {public:    static const bool has_denorm = false;    static const bool has_denorm_loss = false;    static const bool has_infinity = false;    static const bool has_quiet_NaN = false;    static const bool has_signaling_NaN = false;    static const bool is_bounded = false;    static const bool is_exact = false;    static const bool is_iec559 = false;    static const bool is_integer = false;    static const bool is_modulo = false;    static const bool is_signed = false;    static const bool is_specialized = false;    static const bool tinyness_before = false;    static const bool traps = false;    static const float_round_style round_style = round_toward_zero;    static const int digits = 0;    static const int digits10 = 0;    static const int max_exponent = 0;    static const int max_exponent10 = 0;    static const int min_exponent = 0;    static const int min_exponent10 = 0;    static const int radix = 0;    static T denorm_min() throw();    static T epsilon() throw();    static T infinity() throw();    static T max() throw();    static T min() throw();    static T quiet_NaN() throw();    static T round_error() throw();    static T signaling_NaN() throw();    };
这个类的子重要的用途就是可以用它的成员函数来判断一个类型(例如:int ,long )的取值范围。该类是一个模板类。其用法如下:

#include<iostream>#include<string>#include <limits>using namespace std;int main(){cout<<"------long类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(long)<<endl;cout<<"最大值:"<<numeric_limits<long>::max()<<"\t";cout<<"最小值:"<<numeric_limits<long>::min()<<endl;cout<<"-----int 类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(int)<<endl;cout<<"最大值:"<<numeric_limits<int>::max()<<"\t";cout<<"最小值:"<<numeric_limits<int>::min()<<endl;cout<<"-----unsigned int类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(unsigned int)<<endl;cout<<"最大值:"<<numeric_limits<unsigned int>::max()<<"\t";cout<<"最小值:"<<numeric_limits<unsigned int>::min()<<endl;cout<<"------_int32类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(_int32)<<endl;cout<<"最大值:"<<numeric_limits<_int32>::max()<<"\t";cout<<"最小值:"<<numeric_limits<_int32>::min()<<endl;cout<<"------unsigned _int32 类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(unsigned _int32 )<<endl;cout<<"最大值:"<<numeric_limits<unsigned _int32 >::max()<<"\t";cout<<"最小值:"<<numeric_limits<unsigned _int32 >::min()<<endl;cout<<"-----float类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(float)<<endl;cout<<"最大值:"<<numeric_limits<float>::max()<<"\t";cout<<"最小值:"<<numeric_limits<float>::min()<<endl;cout<<"-----double类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(double)<<endl;cout<<"最大值:"<<numeric_limits<double>::max()<<"\t";cout<<"最小值:"<<numeric_limits<double>::min()<<endl;cout<<"------long long类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(long long)<<endl;cout<<"最大值:"<<numeric_limits<long long>::max()<<"\t";cout<<"最小值:"<<numeric_limits<long long>::min()<<endl;cout<<"------long double类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(long double)<<endl;cout<<"最大值:"<<numeric_limits<long double>::max()<<"\t";cout<<"最小值:"<<numeric_limits<long double>::min()<<endl;cout<<"------ldouble long类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(double long)<<endl;cout<<"最大值:"<<numeric_limits<double long>::max()<<"\t";cout<<"最小值:"<<numeric_limits<double long>::min()<<endl;    cout<<"------_int64类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(_int64)<<endl;cout<<"最大值:"<<numeric_limits<_int64>::max()<<"\t";cout<<"最小值:"<<numeric_limits<_int64>::min()<<endl;cout<<"------unsigned _int64 类型的最大值和最小值及所占用字节数-------:"<<endl;cout<<"字节数:"<<sizeof(unsigned _int64 )<<endl;cout<<"最大值:"<<numeric_limits<unsigned _int64 >::max()<<"\t";cout<<"最小值:"<<numeric_limits<unsigned _int64 >::min()<<endl;}

输出结果如下:

------long类型的最大值和最小值及所占用字节数-------:字节数:4最大值:2147483647      最小值:-2147483648-----int 类型的最大值和最小值及所占用字节数-------:字节数:4最大值:2147483647      最小值:-2147483648-----unsigned int类型的最大值和最小值及所占用字节数-------:字节数:4最大值:4294967295      最小值:0------_int32类型的最大值和最小值及所占用字节数-------:字节数:4最大值:2147483647      最小值:-2147483648------unsigned _int32 类型的最大值和最小值及所占用字节数-------:字节数:4最大值:4294967295      最小值:0-----float类型的最大值和最小值及所占用字节数-------:字节数:4最大值:3.40282e+038    最小值:1.17549e-038-----double类型的最大值和最小值及所占用字节数-------:字节数:8最大值:1.79769e+308    最小值:2.22507e-308------long long类型的最大值和最小值及所占用字节数-------:字节数:8最大值:9223372036854775807     最小值:-9223372036854775808------long double类型的最大值和最小值及所占用字节数-------:字节数:8最大值:1.79769e+308    最小值:2.22507e-308------ldouble long类型的最大值和最小值及所占用字节数-------:字节数:8最大值:1.79769e+308    最小值:2.22507e-308------_int64类型的最大值和最小值及所占用字节数-------:字节数:8最大值:9223372036854775807     最小值:-9223372036854775808------unsigned _int64 类型的最大值和最小值及所占用字节数-------:字节数:8最大值:18446744073709551615    最小值:0请按任意键继续. . .

有上面的结果可知:

long和int具有相同的字节数和取值范围.         2147483647 ~~~~ -2147483648
int和_int32具有相同的字节数和取值范围.         2147483647 ~~~~ -2147483648
unsigned int 和unsigned _int32具有相同的字节数和取值范围. 0~~~~~~ 4294967295
long long 和 _int64 具有相同的字节数和取值范围.    -9223372036854775808~~~~~~~~9223372036854775807
long double 和 double long 具有相同的含义。   2.22507e-308~~~~~~~~~1.79769e+308

0 0