1 std::numeric_limits简介

std::numeric_limitsC++标准库提供的查询特定数据类型属性的模型函数,此属性包括数据类型的最大值、最小值等,比如获取float型的最大值、最小值等。

函数声明如下

template< class T > class numeric_limits;

头文件为<limits>

这个函数的作用就类似于C中各种对数据类型边界的宏定义,比如INT_MININT_MAX等。

官方文档页面:https://en.cppreference.com/w/cpp/types/numeric_limits

2 std::numeric_limits的函数

std::numeric_limits包含了以下的常用函数:

  • max() : 返回指定类型最大有限值
  • min():返回指定类型的最小的有限值
  • lowest():返回指定类型的最低有限值

3 std::numeric_limits的使用

#include <iostream>
#include <limits>

int main()
{
    std::cout << "-----short-----" << std::endl;
    std::cout << "max = " << std::numeric_limits<short>::max() << std::endl;
    std::cout << "min = " << std::numeric_limits<short>::min() << std::endl;
    std::cout << "lowest = " << std::numeric_limits<short>::lowest() << std::endl;
    std::cout << "epsilon = " << std::numeric_limits<short>::epsilon() << std::endl;

    std::cout << "-----int-----" << std::endl;
    std::cout << "max = " << std::numeric_limits<int>::max() << std::endl;
    std::cout << "min = " << std::numeric_limits<int>::min() << std::endl;
    std::cout << "lowest = " << std::numeric_limits<int>::lowest() << std::endl;
    std::cout << "epsilon = " << std::numeric_limits<int>::epsilon() << std::endl;

    std::cout << "-----unsigned int-----" << std::endl;
    std::cout << "max = " << std::numeric_limits<unsigned int>::max() << std::endl;
    std::cout << "min = " << std::numeric_limits<unsigned int>::min() << std::endl;
    std::cout << "lowest = " << std::numeric_limits<unsigned int>::lowest() << std::endl;
    std::cout << "epsilon = " << std::numeric_limits<unsigned int>::epsilon() << std::endl;

    std::cout << "-----float-----" << std::endl;
    std::cout << "max = " << std::numeric_limits<float>::max() << std::endl;
    std::cout << "min = " << std::numeric_limits<float>::min() << std::endl;
    std::cout << "lowest = " << std::numeric_limits<float>::lowest() << std::endl;
    std::cout << "epsilon = " << std::numeric_limits<float>::epsilon() << std::endl;

    std::cout << "-----double-----" << std::endl;
    std::cout << "max = " << std::numeric_limits<double>::max() << std::endl;
    std::cout << "min = " << std::numeric_limits<double>::min() << std::endl;
    std::cout << "lowest = " << std::numeric_limits<double>::lowest() << std::endl;
    std::cout << "epsilon = " << std::numeric_limits<double>::epsilon() << std::endl;

    std::cout << "-----long-----" << std::endl;
    std::cout << "max = " << std::numeric_limits<long>::max() << std::endl;
    std::cout << "min = " << std::numeric_limits<long>::min() << std::endl;
    std::cout << "lowest = " << std::numeric_limits<long>::lowest() << std::endl;
    std::cout << "epsilon = " << std::numeric_limits<long>::epsilon() << std::endl;

    std::cout << "-----long long-----" << std::endl;
    std::cout << "max = " << std::numeric_limits<long long>::max() << std::endl;
    std::cout << "min = " << std::numeric_limits<long long>::min() << std::endl;
    std::cout << "lowest = " << std::numeric_limits<long long>::lowest() << std::endl;
    std::cout << "epsilon = " << std::numeric_limits<long long>::epsilon() << std::endl;


    return 0;
}

输出

-----short-----
max = 32767
min = -32768
lowest = -32768
epsilon = 0
-----int-----
max = 2147483647
min = -2147483648
lowest = -2147483648
epsilon = 0
-----unsigned int-----
max = 4294967295
min = 0
lowest = 0
epsilon = 0
-----float-----
max = 3.40282e+38
min = 1.17549e-38
lowest = -3.40282e+38
epsilon = 1.19209e-07
-----double-----
max = 1.79769e+308
min = 2.22507e-308
lowest = -1.79769e+308
epsilon = 2.22045e-16
-----long-----
max = 2147483647
min = -2147483648
lowest = -2147483648
epsilon = 0
-----long long-----
max = 9223372036854775807
min = -9223372036854775808
lowest = -9223372036854775808
epsilon = 0

4 在什么场景下需要使用std::numeric_limits

对于有些比较特殊的变量,经常使用的初始化值0就不太适合,比如说一个最简单的场景,找出一个std::vector<float>中的最大值,这个容器中有正值有负值,那么我们需要初始化一个float max,那么在这种情况下就不能使用0作为初始化值,而应该使用std::numeric_limits<int>::epsilon()作为初始化值。