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
相关推荐
承渊政道1 小时前
C++学习之旅【C++伸展树介绍以及红黑树的实现】
开发语言·c++·笔记·b树·学习·visual studio
郭涤生1 小时前
C++中设置函数与回调函数设值的性能差异及示例
开发语言·c++
mjhcsp1 小时前
C++ 爬山算法(Hill Climbing):局部搜索(Local Search)的核心解析
c++·爬山算法
柏木乃一1 小时前
Linux线程(7)基于策略模式的日志模块
linux·运维·服务器·c++·线程·策略模式
TrueDei1 小时前
linux-C/C++主子进程同时占用主进程文件描述符问题
linux·c语言·c++
仰泳的熊猫2 小时前
题目2266:蓝桥杯2015年第六届真题-打印大X
数据结构·c++·算法·蓝桥杯
IT界的老黄牛2 小时前
【IT老齐230 笔记 + 思考】金融业容灾方案“两地三中心“是什么意思?
数据库·笔记·架构
testresultstomorrow2 小时前
GitHub 代码上传与故障排除实战指南
经验分享·笔记·开源·github
cui_ruicheng2 小时前
C++ 数据结构:AVL树原理与实现
数据结构·c++