包含头文件:#include<limits>

它是一个模板类,它主要是把C++当中的一些内建型别进行了封装,比如说numeric_limits<int>是一个特化后的类,从这个类的成员变量与成员函数中,我们可以了解到int的很多特性:可以表示的最大值,最小值,是否是精确的,是否是有符号等等。如果用其他任意(非内建类型)来特化这个模板类,比如string,string怎么可能有最大值?我们从MSDN上可以了解到,这对string,成员变量与成员函数是没有意义的,要么返回0要么为false。

一般来说,数值类型的极值是一个与平台相关的特性。c++标准程序库通过template numeric_limits提供这些极值,取代传统C语言所采用的预处理常数。你仍然可以使用后者,其中整数常数定义于<climits>和<limits.h>,浮点常数定义于<cfloat>和<float.h>,新的极值概念有两个优点,一是提供了更好的类型安全性,二是程序员可借此写出一些template以核定这些极值。

下面是numeric_limits定义

下面是参数的解释

digits10

返回目标类型在十进制下可以表示的最大位数

epsilon

返回目标数据类型能表示的最逼近1的正数和1的差的绝对值

has_denorm

测试目标类型是不是可以非规范化表示示

has_denorm_loss

测试所有类型是不是能测出因为非规范化而造成的精度损失(不是因为结果本身的不精确)

has_infinity

测试目标类型是不是能表示无限(比如被0除,或者其他一些情况)

has_quiet_NaN

检查目标类型是不是支持安静类型的NaN

has_signaling_NaN

检查目标类型是不是支持信号类型的NaN

infinity

检查目标类型的无限类型(如果支持无限表示)

is_bounded

检查目标类型的取值是否有限

is_exact

测试目标类型的计算结果是不是不会造成舍入误差(比如float是0)

is_iec559

测试目标类型是不是符合IEC559标准

is_integer

测试目标类型是不是可以用整型来表示(比如char是1,float是0)

is_modulo

Tests if a type has a modulo representation.

is_signed

测试目标类型是否是带符号的

is_specialized

测试目标类型是不是在numeric_limits .模板类中有特殊定义

max

返回可取的有限最大值

max_exponent

Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.

max_exponent10

Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.

min

返回可取的最小值(规范化)

min_exponent

Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.

min_exponent10

Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.

quiet_NaN

返回目标类型的安静NAN的表示

radix

Returns the integral base, referred to as radix, used for the representation of a type.

round_error

返回目标类型的最大可能的舍入误差

round_style

Returns a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value.

signaling_NaN

返回目标类型关于信号NAN的表示

tinyness_before

测试目标类型是不是能测定出微小的舍入误差

traps

Tests whether trapping that reports on arithmetic exceptions is implemented for a type.


例:输出int、float、char、long、double、signed、unsigned、long long 的最大最小值(float中表示的是所能表示的最小值)

代码:

#include<iostream>
#include <limits>
using namespace std;
int main()
{
cout <<"TYPENAME min max"<<endl;
cout <<"==========================================================================="<<endl;
cout <<"char"<<" ";
cout << (int)numeric_limits<char>::min() <<" ";
cout << (int)numeric_limits<char>::max() <<endl;
cout <<"short"<<" ";
cout << numeric_limits<short>::min() <<" ";
cout << numeric_limits<short>::max() <<endl;
cout <<"int"<<" ";
cout << numeric_limits<int>::min() <<" ";
cout << numeric_limits<int>::max() <<endl;
cout <<"long"<<" ";
cout << numeric_limits<long>::min() <<" ";
cout << numeric_limits<long>::max() <<endl;
cout <<"float"<<" ";
cout << numeric_limits<float>::min() <<" ";
cout << numeric_limits<float>::max() <<endl;
cout <<"double"<<" ";
cout << numeric_limits<double>::min() <<" ";
cout << numeric_limits<double>::max() <<endl;
cout <<"unsigned"<<" ";
cout << numeric_limits<unsigned>::min() <<" ";
cout << numeric_limits<unsigned>::max() <<endl;
cout <<"long long"<<" ";
cout << numeric_limits<long long>::min() <<" ";
cout << numeric_limits<long long>::max() <<endl;
system("pause");
return 0;
}

C++标准库的数值极限numeric_limits的更多相关文章

  1. C语言的本质(26)——C标准库之数值字符串转换

    C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. #include <stdlib.h> int atoi(const char *nptr); a ...

  2. C标准库-数值字符串转换与内存分配函数

    原文链接:http://www.orlion.ga/977/ 一.数值字符串转换函数 #include <stdlib.h> int atoi(const char *nptr); dou ...

  3. 《挑战30天C++入门极限》C++的iostream标准库介绍(3)

        C++的iostream标准库介绍(3) C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出. 1.有流对象的成员函 ...

  4. 《挑战30天C++入门极限》C++的iostream标准库介绍(2)

        C++的iostream标准库介绍(2) 接下来我们继续看一下C++风格的串流控制,C++引入了ostringstream.istringstream.stringstream这三个类,要使用 ...

  5. 《挑战30天C++入门极限》C++的iostream标准库介绍(1)

        C++的iostream标准库介绍(1) 我们从一开始就一直在利用C++的输入输出在做着各种练习,输入输出是由iostream库提供的,所以讨论此标准库是有必要的,它与C语言的stdio库不同 ...

  6. 用robotframework 标准库String解决由于存在千分位分隔符导致两个数值不相等的问题。

    在编写robotframework自动化断言的过程中,我遇到了如下问题: 我想写一个两个金额判断是否相等的断言,其中一个金额是展示字段存在千分位分隔符,另一个金额是input带入字段,没有千分位分隔符 ...

  7. 9.Python3标准库--数据压缩与归档

    ''' 尽管现代计算机系统的存储能力日益增长,但生成数据的增长是永无休止的. 无损(lossless)压缩算法以压缩或解压缩数据花费的时间来换取存储数据所需要的空间,以弥补存储能力的不足. Pytho ...

  8. 6.Python3标准库--数学运算

    ''' 作为一种通用的变成语言,Python经常用来解决数学问题.它包含一些用于管理整数和浮点数的内置类型,这很适合完成一般应用中可能出现的基本数学运算. 而标准库中包含一些用于满足更高级需求的模块. ...

  9. python常用标准库(时间模块 time和datetime)

    常用的标准库 time时间模块 import time time -- 获取本地时间戳 时间戳又被称之为是Unix时间戳,原本是在Unix系统中的计时工具. 它的含义是从1970年1月1日(UTC/G ...

随机推荐

  1. Unity NGUI UILabel文字变色 及相关问题

    在同一个UILabel中可以有不同颜色的文字只需要添加BBCode标记[ff0000]Red Label[-],那么在这个标记之间的RedLabel 就会变成红色 注意: 1.文本最终显示的颜色=Co ...

  2. EnterpriseArchitectect 软件的勾选的几个选项对应的中文意思

    Business Process 业务流程 Requirements 需求分析 Use Case 用例 Domain Model 领域模型 Class 类 Database 数据库设计 Compone ...

  3. 基于Lucene3.5.0怎样从TokenStream获得Token

    通过学习Lucene3.5.0的doc文档,对不同release版本号 lucene版本号的API修改做分析.最后找到了有价值的修改信息. LUCENE-2302: Deprecated TermAt ...

  4. Android ADT离线更新办法

    Troubleshooting ADT Installation If you are having trouble downloading the ADT plugin after followin ...

  5. [PWA] Keynote: Progressive Web Apps across all frameworks

    PWA: Add to home screen Angular Universal Server side rendering: for achieving better proference on ...

  6. android 21 隐式意图启动系统预定义activity

    Intent intent=new Intent(LoginActivity.this, MainActivity.class);//显示意图启动,显示从一个activity到另一个activity, ...

  7. Qt 学习之路:自定义事件

    尽管 Qt 已经提供了很多事件,但对于更加千变万化的需求来说,有限的事件都是不够的.例如,我要支持一种新的设备,这个设备提供一种崭新的交互方式,那么,这种事件如何处理呢?所以,允许创建自己的事件 类型 ...

  8. 亲测PHP环境

    一.安装Apache2.2.22→1.下载软件,点安装 2.填写dengguoxing.com  www.dengguoxing.com(暂时不知道什么用)3.custom 个性化安装 更改路径即可 ...

  9. 自定义 textField 的清除 button

    UIButton *clearButton = [self.textField valueForKey:@"_clearButton"]; [clearButton setImag ...

  10. 模拟dispatch_once

    dispatch_once   dispatch_once可以保证一段代码只被执行一次,因此出现之后使用最多的场景就是实现单例.本文来模拟实现dispatch_once的功能. 模拟dispatch_ ...