📌  相关文章
📜  std :: numeric_limits之间的区别<T>C++中的最小,最大和最低

📅  最后修改于: 2021-05-30 11:02:13             🧑  作者: Mango

在极限头中的std :: numeric_limits 类提供分钟(),MAX(),最低()函数的所有数字数据类型与其他成员函数沿。

std :: numeric_limits :: max():任何类型Tstd :: numeric_limits :: max()给出由数字T表示的最大有限值。因此,函数max()给出数据类型T的值x ,使得在y> x的情况下没有其他有限值y。

对于整数类型和浮点数据类型,函数max()都给出了可以表示的最大值,并且在数字行上该值的右边没有其他值。

std :: numeric_limits :: lowest():任何类型Tstd :: numeric_limits :: lowest()是数字类型T可以表示的最低有限值,因此没有其他有限值y,其中y> x

对于整数类型和浮点数据类型,函数lowest()都给出了可以表示的最小值,并且在数字行上该值的左侧没有其他值。函数lowest()基本上是max()的负值。

std :: numeric_limits :: min():任何类型Tstd :: numeric_limits :: min()是可由数字类型T表示的最小有限值。因此,函数min()可以由类型T表示的最小的可能值。

对于具有非规格化的浮点类型,函数min()返回最小的正规格化值。由于函数min()返回浮点类型的最小正归一化值,因此该值的指数不能为0

要获得最小正非正规值,请使用std :: numeric_limits :: denorm_min()denorm_min()仅适用于浮点类型,而对于整数类型,其值为0。

对于整数类型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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”