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
相关推荐
Zhan8611242 小时前
数据接口的序列号机制与丢包检测:西班牙行情数据IBEX指数实时行情接入笔记
大数据·数据结构·笔记·区块链
unicrom_深圳市由你创科技8 小时前
哪些控制逻辑应该放在 PLC,哪些放在上位机?
c++
玖玥拾10 小时前
C/C++ 基础笔记(十三)继承
c语言·c++·继承
闪闪发亮的小星星10 小时前
开普勒三大定律
笔记
ao-weilai11 小时前
C++:哈希表
c++·哈希算法·散列表
汉克老师11 小时前
GESP7级C++考试语法知识(二、指数函数(1、pow() 函数)
c++·指数函数·pow·gesp7级·精度误差
自传.11 小时前
尚硅谷 Vibe Coding|第一章 AI 编程基础理论 学习笔记
笔记·学习·尚硅谷·vibe coding
旖-旎11 小时前
FloodFill(图像渲染)(1)
c++·算法·深度优先·力扣
汉克老师13 小时前
GESP2026年3月认证C++六级真题与解析(编程题1 选数)
c++·动态规划·线性dp·gesp六级·状态转移·选与不选
有点。13 小时前
C++倍增法(练习题)
c++·算法