在极限头中的std :: numeric_limits
std :: numeric_limits
对于整数类型和浮点数据类型,函数max()都给出了可以表示的最大值,并且在数字行上该值的右边没有其他值。
std :: numeric_limits
对于整数类型和浮点数据类型,函数lowest()都给出了可以表示的最小值,并且在数字行上该值的左侧没有其他值。函数lowest()基本上是max()的负值。
std :: numeric_limits
对于具有非规格化的浮点类型,函数min()返回最小的正规格化值。由于函数min()返回浮点类型的最小正归一化值,因此该值的指数不能为0 。
要获得最小正非正规值,请使用std :: numeric_limits
对于整数类型min() ,函数lowest ()的别名,这意味着两者都给出相同的最低有限值。如果已经定义了类似于浮点类型的整数值min(),则该值将为1。
例如:
Type T | Byte size | Function | Binary representation | Value | ||
---|---|---|---|---|---|---|
int | 4 | max() | 01111111111111111111111111111111 | 2147483647 | ||
lowest() | 10000000000000000000000000000000 | -2147483648 | ||||
min() | 10000000000000000000000000000000 | -2147483648 | ||||
denorm_min() | 00000000000000000000000000000000 | 0 | ||||
float | 4 | max() | 0 | 11111110 | 11111111111111111111111 | 3.40282346639e+38 |
lowest() | 1 | 11111110 | 11111111111111111111111 | -3.40282346639e+38 | ||
min() | 0 | 00000001 | 00000000000000000000000 | 1.17549435082e-38 | ||
0 | 00000000 | 00000000000000000000001 | 1.40129846432e-45 | |||
denorm_min() |
下面是说明上述概念的程序:
C++
// C++ program to illustrate difference
// between the numeric limits min, max,
// and the lowest
#include
#include
#include
using namespace std;
// Driver Code
int main()
{
int x;
// Size of int
cout << "int " << sizeof(int)
<< " bytes" << endl;
// numeric_limits::max()
x = numeric_limits::max();
cout << "\tmax :" << endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< x << endl;
// numeric_limits::lowest()
x = numeric_limits::lowest();
cout << "\tlowest :" << endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< x << endl;
// numeric_limits::min()
x = numeric_limits::min();
cout << "\tmin :" << endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< x << endl;
// numeric_limits::denorm_min()
x = numeric_limits::denorm_min();
cout << "\tdenorm_min :" << endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< x << endl;
cout << endl;
// Size of float
cout << "float " << sizeof(float)
<< " bytes" << endl;
float f;
// numeric_limits::max()
f = numeric_limits::max();
// read the binary representation
// of the float as an integer
x = *(int*)&f;
cout << "\tmax :" << endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< f << endl;
// numeric_limits::lowest()
f = numeric_limits::lowest();
x = *(int*)&f;
cout << "\tlowest :" << endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< f << endl;
// numeric_limits::min()
f = numeric_limits::min();
x = *(int*)&f;
cout << "\tmin :" << endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< f << endl;
// numeric_limits::denorm_min()
f = numeric_limits::denorm_min();
x = *(int*)&f;
cout << "\tdenorm_min :" << endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< f << endl;
return 0;
}
C++
// C++ program to illustrate the
// above concepts
#include
#include
#include
#include
using namespace std;
// Driver Code
int main()
{
// Output is not exact
cout << "\tlowest :" << endl
<< numeric_limits::lowest()
<< endl;
// The value from the output
float f = -3.40282e+38;
int x = *(int*)&f;
cout << bitset<8 * sizeof(x)>(x)
<< endl
<< endl;
// Correct value
f = numeric_limits::lowest();
x = *(int*)&f;
cout << "\tnumeric_limits::lowest() :"
<< endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< endl;
cout << "\tusing setprecision:"
<< endl;
// output is more precise
cout << setprecision(10) << f << endl;
// The value from the output
f = -3.402823466e+38;
// Read the binary representation
// of the float as an integer
x = *(int*)&f;
cout << bitset<8 * sizeof(x)>(x)
<< endl;
return 0;
}
int 4 bytes
max :
01111111111111111111111111111111
2147483647
lowest :
10000000000000000000000000000000
-2147483648
min :
10000000000000000000000000000000
-2147483648
denorm_min :
00000000000000000000000000000000
0
float 4 bytes
max :
01111111011111111111111111111111
3.40282e+38
lowest :
11111111011111111111111111111111
-3.40282e+38
min :
00000000100000000000000000000000
1.17549e-38
denorm_min :
00000000000000000000000000000001
1.4013e-45
注意:在C++中,默认精度为6,这意味着最多使用6个有效数字来表示数字,因此,实际数字将与打印值不同。要获取实际值,请尝试设置更高的精度。
下面是说明相同内容的程序:
C++
// C++ program to illustrate the
// above concepts
#include
#include
#include
#include
using namespace std;
// Driver Code
int main()
{
// Output is not exact
cout << "\tlowest :" << endl
<< numeric_limits::lowest()
<< endl;
// The value from the output
float f = -3.40282e+38;
int x = *(int*)&f;
cout << bitset<8 * sizeof(x)>(x)
<< endl
<< endl;
// Correct value
f = numeric_limits::lowest();
x = *(int*)&f;
cout << "\tnumeric_limits::lowest() :"
<< endl
<< bitset<8 * sizeof(x)>(x)
<< endl
<< endl;
cout << "\tusing setprecision:"
<< endl;
// output is more precise
cout << setprecision(10) << f << endl;
// The value from the output
f = -3.402823466e+38;
// Read the binary representation
// of the float as an integer
x = *(int*)&f;
cout << bitset<8 * sizeof(x)>(x)
<< endl;
return 0;
}
lowest :
-3.40282e+38
11111111011111111111111111101110
numeric_limits::lowest() :
11111111011111111111111111111111
using setprecision:
-3.402823466e+38
11111111011111111111111111111111