首页 > 编程语言 >C++(STL):06---数值的极值(numeric_limits类)

C++(STL):06---数值的极值(numeric_limits类)

时间:2022-11-01 14:35:45浏览次数:37  
标签:06 cout limits STL max numeric int long

一、数值的极值概述

  • 数值类型有着与平台相依的极值
  • C++标准规定了各种类型必须保证的最小精度。这些最小值如下图所示:
  •  


类型


最小长度


char


1byte(8bits)


shortint


2bytes


int


2bytes


longint


4bytes


longlongint


8bytes


float


4bytes


double


8bytes


longdouble


8bytes

 

二、numeric_limits

  • 传统C语言使用预处理器常量来决定数值的极值,其中整数常量定义于<climits>或<limits.h>中,浮点常量定义于<cfloat>或<float.h>中
  • C++标准库定义一个template numeric_limits来提供这些常值
  • 使用numeric_limits有优点:
  • 第一个是提供更好的类型安全性
  • 第二个是程序员可以借此写出一些template以核定这些极值
  • 使用numeric_limits可以很轻松的写出跨平台的程序

三、numeric_limits的实现

  • numeric_limits实现有两种:
  • 一种是通用性的模板,其提供很多接口,是每个类型都共用的
  • 一种是特化版本的,每特定的类型特例化 

 

  • 这里把成员is_specialized设为false,意思为对于类型T而言,不存在所谓的极值

特化版本的numeric_limits

  • 各具体类型的极值,由特化版本提供。定义如下:
namespace std {
// numeric limits for int
template<> class numeric_ limits<int> {
public:
// yes, a specialization for numeric limits of int does exist static constexpr bool is_ specialized = true;
static constexpr int min() noexcept {
return -2147483648;
}
static constexpr int max() noexcept {
return 2147483647 ;
}
static constexpr int digits = 31;
};


  • 这里把is_specialized设为true,所有其他成员都根据特定类型的具体极值而设定
  • 特化版本涵盖所有数值基础类型,包括:bool、char、signed char、unsigned char、char16_t、char32_t、wchar_t、short、unsigned short、int、unsigned int、long、unsigned long、long long、unsigned long long、float、double、long double
  • 当然你也可以为自己定义的数值类型定义一份特例化 

 

四、numeric_limits提供的操作

  • numeric_limits定义在<limits>头文件中,下图列出了所有成员及其意义,最右侧对应的是C常量

  • C++11前并不提供lowest()和max_digits10,且所有成员函数不提供noexcept

所有成员都是constexpr的

  • 从C++11起,所有成员都被声明为constexpr的
  • 例如你可以在需要编译期表达式的地方使用max():
  1. static const int ERROR_VALUE = std::numeric_limits<int>::max();
  2. float a[std::numeric_limits<short>::max()];

round_style、has_denorm

  • round_style的值如下图所示:

舍/入风格

意义(译注:以下说明中的y为“被操作数")

round_toward_zero

无条件舍去(译注:就是truncate)

round_to_nearest

化为最接近值(译注:就是四舍五人)

round_toward_infinity

化为不小于y之最小整数 

ceiling)

round_toward_neg_infinity

化为不大于y之最大整数 

round_indeterminate

无法确定


五、numeric_limits使用示例

演示案例1

  • 查看各种数据类型的最大值
  1. cout << "max(short)" << numeric_limits<short>::max() << endl;
  2. cout << "max(int)" << numeric_limits<int>::max() << endl;
  3. cout << "max(long)" << numeric_limits<long>::max() << endl << endl;

  4. cout << "max(float)" << numeric_limits<float>::max() << endl;
  5. cout << "max(double)" << numeric_limits<double>::max() << endl;
  6. cout << "max(long double)" << numeric_limits<long double>::max() << endl << endl;
  • x64编译器下结果如图所示 
max(short)32767
max(int)2147483647
max(long)2147483647
max(float)3.40282e+38
max(double)1.79769e+308
max(long double)1.79769e+308


演示案例2

  •  下面查看char类型是否带有正负号,string类型是否带有极值
cout << boolalpha;
cout << "is_signed(char)" <<
numeric_limits<char>::is_signed << endl;
cout << "is_specialized(string)" <<
numeric_limits<string>::is_specialized << endl;



is_signed(char)true 
is_specialized(string)false



标签:06,cout,limits,STL,max,numeric,int,long
From: https://blog.51cto.com/u_14934686/5813628

相关文章

  • C++(STL):05---智能指针之unique_ptr
    一、unique_ptr类头文件:#include<memory>智能指针,是一个模板。创建智能指针时,必须提供指针所指的类型与shared_ptr的不同之处:shared_ptr所指向的对象可以有多个其他shared_p......
  • ANT+ 自行车车灯 数据页面6–辅助灯光模式支持(0x06)
    ANT+控制器可以请求此页面(如5.3节中所述),以确定每个子灯支持哪些自定义模式。当ANT+控制器请求时,数据页6从ANT+自行车灯广播。任何带有副灯的ANT+自行车灯均应支持此页面。该......
  • 动力节点—day06
    常用类StringString表示字符串类型,属于引用数据类型,不属于基本数据类型在Java中随便使用双引号括起来的都是String对象,例如“abc”、“def”,“helloworld!”,这是三个S......
  • STTH15RQ06-Y-ASEMI代理意法超快恢复二极管STTH15RQ06-Y
    编辑-ZSTTH15RQ06-Y用的TO-220-2L封装,是意法一款汽车用超快恢复高压二极管。STTH15RQ06-Y的反向漏电流(IR)为20uA,其工作时耐温度范围为-40~175摄氏度。STTH15RQ06-Y的浪涌非......
  • day06-(mysql)
    建表:CREATEDATABASEmysqltest2;USEmysqltest2;--部门表CREATETABLEDEPT(DEPTNOINTPRIMARYKEY,--部门编号DNAMEVARCHAR(14),--部门名称L......
  • C++——STL库——reverse、remove、remove_if
    1.reverse函数使用函数功能:将序列[first,last)的元素在原容器中颠倒重排,包含在algorithm库中reverse没有返回值时间复杂度为O(n)示例1:交换vector容器中元素的顺序vect......
  • TR-069第一期第六修正版-11
    3.4.5摘要认证DigestAuthentication本节概述了在CPEWAN管理协议中使用摘要认证的要求。这些要求适用于RPC交换和文件传输的连接验证。请注意,对于不同类型的连接,ACS和CP......
  • 内网安全之:MS14-068 Kerberos 域用户提权漏洞
    内网安全之:MS14-068Kerberos域用户提权漏洞目录内网安全之:MS14-068Kerberos域用户提权漏洞0漏洞说明(MS14-068;CVE-2014-6324)1pyKEK工具包2MSF中ms14_068利用......
  • 【XSY4206】QWQ(trick)
    两个问题的解决方法感觉很妙:一、给你若干棵树\(T_1,T_2,\cdots,T_k\),设\(f(T_i,u,v)\)为树\(T\)中\(lca(u,v)\)的深度,问如何优美地表示\(g(u,v)=\min_{i=1}^kf(......
  • 06异常
    异常体系结构Java把异常当作对象来处理,定义一个基类java.lang.Throwable作为所有异常的超类Java异常处理机制处理异常自定义异常......