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
相关推荐
天若有情673几秒前
反向封神!C++ 全局单例不避反用,实现无锁多线程函数独占访问
java·javascript·c++
智者知已应修善业7 分钟前
【51单片机调用__TIME__无法实时时间】2023-7-10
c++·经验分享·笔记·算法·51单片机
凤凰院凶涛QAQ16 分钟前
《C++转JAVA快速入手系列》:基本通用语法篇
java·开发语言·c++
Tutankaaa23 分钟前
防震减灾知识竞赛题库:地震常识、应急避险与自救互救指南
经验分享·笔记·学习
wljt30 分钟前
SpringBoot学习笔记五:Spring Boot的web开发
spring boot·笔记·学习
Shadow(⊙o⊙)33 分钟前
C++常见错误解析2.0
开发语言·数据结构·c++·后端·学习·算法
Hello_Embed44 分钟前
嵌入式上位机开发入门(二十八):JSON 与 JsonRPC 入门
网络·笔记·网络协议·tcp/ip·嵌入式
欧米欧44 分钟前
STRING的底层实现
前端·c++·算法
U盘失踪了1 小时前
Playwright codegen脚本录制
笔记
南境十里·墨染春水1 小时前
C++流类库 字符串流
开发语言·c++