• 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

C++ – std::numeric_limits简介与使用,用于获取指定数据类型的最大值与最小值

C++ 发布于2023-01-12 阅读 6,616次 0次评论 0次点赞 本文共3382个字,阅读需要9分钟。

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()作为初始化值。

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++ – std::numeric_limits简介与使用,用于获取指定数据类型的最大值与最小值

原文链接:https://www.stubbornhuang.com/2474/

发布于:2023年01月12日 10:33:51

修改于:2025年04月10日 18:57:13

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
C++ - Windows获取电脑上摄像头设备数目、名字以及id
C++
下一篇
Youtube运营 - 申请开通YPP(Youtube合作伙伴计划)时,人工审核未通过,理由为再利用他人的内容
Youtube运营
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号