C++ – std::numeric_limits简介与使用,用于获取指定数据类型的最大值与最小值
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – std::numeric_limits
原文链接:https://www.stubbornhuang.com/2474/
发布于:2023年01月12日 10:33:51
修改于:2023年01月12日 10:33:51

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()
作为初始化值。
当前分类随机文章推荐
- C++11 - 解析并获取可变参数模板中的所有参数 阅读1008次,点赞0次
- C++ - 常用的C++命令行参数解析第三方库 阅读1716次,点赞2次
- C++ - 随机洗牌算法,std::random_shuffle和std::shuffle 阅读1071次,点赞1次
- C++ - 字节数组byte[]或者unsigned char[]与double的相互转换 阅读1371次,点赞0次
- C++ – UTF8编码下的全角字符转半角字符 阅读1421次,点赞0次
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读799次,点赞0次
- C++11 - 构建一个符合实际应用要求的线程池 阅读977次,点赞0次
- C++ - 在Windows/Linux上创建单级目录以及多级目录的跨平台方法 阅读783次,点赞0次
- C++ - 我的代码风格备忘 阅读644次,点赞0次
- C++ - vector存储动态指针时正确释放内存 阅读5365次,点赞0次
全站随机文章推荐
- Python - 不定长函数参数列表 阅读1824次,点赞0次
- 资源分享 - Advanced global illumination (2nd Edition) 英文PDF下载 阅读5076次,点赞0次
- 资源分享 - Digital Image Processing , Fourth Edition 英文高清PDF下载 阅读2327次,点赞1次
- Unity - Color32[]转IntPtr 阅读2740次,点赞1次
- Pytorch - torch.nn.Conv1d参数详解与使用 阅读1150次,点赞0次
- Pytorch - torch.unsqueeze和torch.squeeze函数 阅读129次,点赞0次
- Mediapipe - 将Mediapipe handtracking封装成动态链接库dll/so,实现在桌面应用中嵌入手势识别功能 阅读8363次,点赞19次
- C++11 - 使用std::thread在类内部以成员函数作为多线程函数执行异步操作 阅读1918次,点赞0次
- 资源分享 - Direct3D Rendering Cookbook 英文高清PDF下载 阅读970次,点赞0次
- 资源分享 - GPU Pro 360 - Guide to Shadows 英文高清PDF下载 阅读2107次,点赞0次
评论
164