C++ – std::numeric_limits简介与使用,用于获取指定数据类型的最大值与最小值
1 std::numeric_limits简介
std::numeric_limits
是C++标准库提供的查询特定数据类型属性的模型函数,此属性包括数据类型的最大值、最小值等,比如获取float型的最大值、最小值等。
函数声明如下
template< class T > class numeric_limits;
头文件为<limits>
。
这个函数的作用就类似于C中各种对数据类型边界的宏定义,比如INT_MIN
、INT_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
修改于:2023年06月21日 17:18:24
当前分类随机文章推荐
- C++ - const_cast, static_cast, dynamic_cast, reinterpret_cast四种cast转换的区别和使用 阅读109次,点赞0次
- C++ - 格式化json字符串,方便展示json字符串的层次结构 阅读3926次,点赞0次
- C++11/std::thread - 可作为线程函数的几种方式总结 阅读3908次,点赞1次
- C++/OpenCV - 详解如何一步步将OpenCV的cv::Mat转换成深度学习模型推理所需的输入数据 阅读411次,点赞0次
- C++ - 我在项目实际开发中用到的第三方库/开源项目,涵盖网络、加密解密、GUI、网络、音视频、图片等等 阅读554次,点赞0次
- C++ - C++使用cuda api获取当前GPU显卡的总共的显存容量、已使用显存容量、剩余显存容量 阅读5899次,点赞2次
- C++ - std::map - 存储动态指针时正确释放内存 阅读4641次,点赞1次
- GCC - 常用手动链接选项-lz、-lrt、-lm、-lc、-lpthread、-lcrypt、dl链接都是什么库? 阅读84次,点赞0次
- C++ - 使用Websocket++编写客户端连接WebSocket服务器并进行通信 阅读5153次,点赞3次
- C++ - 我的代码风格/代码规范备忘 阅读1098次,点赞0次
全站随机文章推荐
- FFmpeg - 将某个文件夹下的图片按标号顺序合成为指定编码格式和指定帧率的视频 阅读5382次,点赞0次
- TensorRT - TensorRT was linked against cuBLAS/cuBLAS LT 11.6.1 but loaded cuBLAS/cuBLAS LT 11.5.4错误提示 阅读396次,点赞0次
- Python - yaml配置用法详解以及使用Pyyaml库操作yaml文件 阅读1645次,点赞0次
- Modern OpenGL - GLSL着色语言4:GLSL中的数据存储限制符 阅读2402次,点赞0次
- Pytorch - 训练网络时出现_pickle.UnpicklingError: pickle data was truncated错误 阅读2278次,点赞1次
- Duilib - 在同一台电脑上限制只能运行一个程序,防止软件多开 阅读2189次,点赞0次
- 资源分享 - Real-Time Collision Detection 英文高清PDF下载 阅读2412次,点赞0次
- TensorRT - 学习和开发的相关网页备忘 阅读39次,点赞0次
- 资源分享 - C++程序设计语言(第1- 3部分),原书第4版 高清PDF下载 阅读4089次,点赞3次
- WordPress - 查询当前登录用户在一天之内的评论总数量 阅读2363次,点赞0次
评论
169