Unlities之numeric_limits

用numeric_limits可以为每一个类型提供一个接口。
A general template provides the default numeric values for any type:
namespace std {
/* general numeric limits as default for any type
*/
template <class T>
class numeric_limits 
{
    public:
    static const bool is_specialized = false;//no specialization for numeric limits exist
    ... //other members that are meaningless for the generalnumeric limits
};
}
? Specializations of the template define the numeric limits for each numeric type as follows:
namespace std {
template<> class numeric_limits<int> 
{
   public:
   static const bool is_specialized = true;//yes, a specialization for numeric limits of int does exist
   static T min() throw() {  return -2147483648;}
   static T max() throw() { return 2147483647;}
   static const int digits = 31;
...
};
}
float的完全特化:
namespace std {
class numeric_limits<float> {
public:
//yes, a specialization for numeric limits of float does exist
static const bool is_specialized = true;
inline static float min() throw() {
return 1.17549435E-38F;
}
inline static float max() throw() {
return 3.40282347E+38F;
}
static const int digits = 24;
static const int digits10 = 6;

static const bool is_signed = true;
static const bool is_integer = false;
static const bool is_exact = false;
static const bool is_bounded = true;
static const bool is_modulo = false;
static const bool is_iec559 = true;
static const int radix = 2;
inline static float epsilon() throw() {
return 1.19209290E-07F;
}
static const float_round_style round_style
= round_to_nearest;
inline static float round_error() throw() {
return 0.5F;
}
static const int min_exponent = -125;
static const int max_exponent = +128;
static const int min_exponentl0 = -37;
static const int max_exponent10 = 38;
static const bool has_infinity = true;
inline static float infinity() throw() { return ...; }
static const bool has_quiet_NaN = true;
inline static float quiet_NaN() throw() { return ...; }
static const bool has_signaling_NaN = true;
inline static float signaling_NaN() throw() { return ...; }
static const float_denorm_style has_denorm = denorm_absent;
static const bool has_denorm_loss = false;
inline static float denorm_rain() throw() { return min(); }
static const bool traps = true;
static const bool tinyness_before = true;
};
}
这上面的代码中应该可以知道numeric_limits是干什么的了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值