Unlities之numeric_limits

来源:互联网 发布:工商银行网络银行 编辑:程序博客网 时间:2024/04/30 21:45
用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
原创粉丝点击