C++笔记:std::numeric_limits

std::numeric_limits 是 C++ 标准库里一个 用来查询数值类型"性质"的模板类

简单说:

它提供某个数值类型的各种边界和特性信息(最大值、最小值、精度、是否有无穷等)。

cpp 复制代码
#include <limits>
template<class T>
class std::numeric_limits;

通过 numeric_limits 模板的特化提供此信息。标准库为所有算术类型制定可用的特化:

cpp 复制代码
template<> class numeric_limits<bool>;
   

template<> class numeric_limits<char>;
   

template<> class numeric_limits<signed char>;
   

template<> class numeric_limits<unsigned char>;
   

template<> class numeric_limits<wchar_t>;
   

template<> class numeric_limits<char8_t>;
  (C++20 起) 

template<> class numeric_limits<char16_t>;
  (C++11 起) 

template<> class numeric_limits<char32_t>;
  (C++11 起) 

template<> class numeric_limits<short>;
   

template<> class numeric_limits<unsigned short>;
   

template<> class numeric_limits<int>;
   

template<> class numeric_limits<unsigned int>;
   

template<> class numeric_limits<long>;
   

template<> class numeric_limits<unsigned long>;
   

template<> class numeric_limits<long long>;
  (C++11 起) 

template<> class numeric_limits<unsigned long long>;
  (C++11 起) 

template<> class numeric_limits<float>;
   

template<> class numeric_limits<double>;
   

template<> class numeric_limits<long double>; 

另外,对于每个存在特化的 cv 无限定类型的每个 cv 限定版本存在特化,等同于非限定的特化,例如提供 std::numeric_limits<const int>std::numeric_limits<volatile int>std::numeric_limits<const volatile int> ,且它们等价于 std::numeric_limits<int>

成员常量

成员 类型 含义
is_specialized bool 是否为该类型提供了 numeric_limits 特化
is_signed bool 是否是有符号类型
is_integer bool 是否是整数类型
is_exact bool 是否能精确表示(整数为 true,浮点为 false)
has_infinity bool 是否支持 +∞
has_quiet_NaN bool 是否支持 quiet NaN
has_signaling_NaN bool 是否支持 signaling NaN
has_denorm_loss bool 是否会检测 denorm 精度损失
is_iec559 bool 是否符合 IEEE 754
is_bounded bool 是否有有限范围
is_modulo bool 是否按模运算处理溢出(如 unsigned)
traps bool 算术运算是否可能产生 trap
tinyness_before bool 是否在舍入前检测 tiny 值
成员 类型 含义
digits int 能无误表示的 radix 位数
digits10 int 能无误表示的 十进制位数
max_digits10 int 保证 round-trip 的十进制位数
radix int 数值表示使用的 进制基数
min_exponent int 最小正规指数(radix)
min_exponent10 int 最小正规指数(10)
max_exponent int 最大指数(radix)
max_exponent10 int 最大指数(10)
成员 类型 含义
has_denorm std::float_denorm_style 说明
denorm_absent 没有 denormal
denorm_present 支持 denormal
denorm_indeterminate 实现未指定
round_style std::float_round_style 说明
round_to_nearest 最近值舍入(IEEE754默认)
round_toward_zero 向0舍入
round_toward_infinity 向 +∞
round_toward_neg_infinity 向 -∞

成员函数

都返回T类型

成员函数 含义
min 返回类型 T最小有限值整数类型:返回最小值 浮点类型 :返回 最小正正规数(smallest positive normalized value)
lowest 返回 最小的有限值(最负的值)
max 返回该类型 最大有限值
epsilon 1下一个可表示浮点数之间的差
round_error 返回 最大舍入误差(maximum rounding error)。 对 IEEE754 浮点来说通常:0.5
infinity 返回 正无穷大 。 仅当:std::numeric_limits<T>::has_infinity == true
quiet_NaN 返回 quiet NaN(安静 NaN)。 不会触发异常;运算会继续传播
signaling_NaN 返回 signaling NaN(发信 NaN)。 用于 触发浮点异常;某些运算会产生 trap 但现实中:很多平台不会真正区分 sNaN
denorm_min 返回 最小正非正规数(subnormal / denormal)

例如

cpp 复制代码
#include <iostream>
#include<limits>
int main()
{
    std::cout<<std::numeric_limits<float>::infinity()<<std::endl;
    std::cout << std::numeric_limits<float>::quiet_NaN() << std::endl;
}
cpp 复制代码
inf
nan
相关推荐
旖-旎5 分钟前
《LeetCode 646 最长数对链 || LeetCode 1143 最长公共子序列》
c++·算法·leetcode·动态规划
kdxiaojie1 小时前
Linux 驱动研究 —— V4L2 (2)
linux·运维·笔记·学习
阿米亚波1 小时前
【C++ STL】std::list
c++·windows·笔记·stl·list
橙色阳光五月天1 小时前
CMake 构建配置文件解析
c++·cmake·plugin
载数而行5201 小时前
C++进阶 Linux相关的静态库,动态库,第三方库
c++
c238561 小时前
下篇:伸缩魔法!进阶吃透可变滑动窗口篇
c++·算法
来一碗刘肉面2 小时前
顺序表的按值查找
数据结构·c++·算法
大明者省2 小时前
‌Docker Desktop - Virtual Machine Platform not enabled
笔记
疯狂打码的少年3 小时前
【软件工程】需求工程:需求获取与需求分析
数据库·笔记·软件工程·需求分析
小肝一下3 小时前
2. 顺序表和链表
c语言·数据结构·c++·链表·dijkstra·依蕾娜·香香的依蕾娜