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
相关推荐
June`1 天前
常量内存和只读缓存
c++·人工智能·算法·cuda
Cx330❀1 天前
【Linux网络】深入 HTTP 协议(五):从 Cookie/Session 原理到 C++ 源码实战
linux·运维·服务器·开发语言·网络·c++·http
鸿芯微控科技1 天前
MFC气体转换系数怎么用?标定气体、换算方向与Python批量计算
c++·python·mfc
晓梦林1 天前
BUUCTF findKey-学习笔记
笔记·学习
hPw0eKIqD1 天前
C++ 模板参数推导问题小记(非推导上下文)
开发语言·c++
程序员爱德华1 天前
Python与C++:异同点对比
c++·python
x17388062731 天前
XXE外部实体注入
xml·笔记
888CC++1 天前
C语言与C++的区别:从面向过程到面向对象
java·c语言·c++
Darkwanderor2 天前
Linux系统编程实战项目:模拟实现shell
linux·c++
himobrinehacken2 天前
揭秘Windows程序启动的神秘之旅
c++·安全