类型 | 最小长度 |
---|---|
char | 1byte(8bits) |
shortint | 2bytes |
int | 2bytes |
longint | 4bytes |
longlongint | 8bytes |
float | 4bytes |
double | 8bytes |
longdouble | 8bytes |
各具体类型的极值,由特化版本提供。定义如下:
namespace std {
// numeric limits for int
template<> class numeric_ limits<int> {
public:
// yes, a specialization for numeric limits of int does exist static constexpr bool is_ specialized = true;
static constexpr int min() noexcept {
return -2147483648;
}
static constexpr int max() noexcept {
return 2147483647 ;
}
static constexpr int digits = 31;
};
static const int ERROR_VALUE = std::numeric_limits<int>::max();float a[std::numeric_limits<short>::max()];
舍/入风格 | 意义(译注:以下说明中的y为“被操作数") |
---|---|
round_toward_zero | 无条件舍去(译注:就是truncate) |
round_to_nearest | 化为最接近值(译注:就是四舍五人) |
round_toward_infinity | 化为不小于y之最小整数 ceiling) |
round_toward_neg_infinity | 化为不大于y之最大整数 |
round_indeterminate | 无法确定 |
cout << "max(short)" << numeric_limits<short>::max() << endl;cout << "max(int)" << numeric_limits<int>::max() << endl;cout << "max(long)" << numeric_limits<long>::max() << endl << endl;
cout << "max(float)" << numeric_limits<float>::max() << endl;cout << "max(double)" << numeric_limits<double>::max() << endl;cout << "max(long double)" << numeric_limits<long double>::max() << endl << endl;
x64编译器下结果如图所示
max(short)32767
max(int)2147483647
max(long)2147483647
max(float)3.40282e+38
max(double)1.79769e+308
max(long double)1.79769e+308
cout << boolalpha;
cout << "is_signed(char)" <<
numeric_limits<char>::is_signed << endl;
cout << "is_specialized(string)" <<
numeric_limits<string>::is_specialized << endl;
is_signed(char)true
is_specialized(string)false