C++标准库的数值极限numeric_limits
包含头文件:#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的更多相关文章
- C语言的本质(26)——C标准库之数值字符串转换
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. #include <stdlib.h> int atoi(const char *nptr); a ...
- C标准库-数值字符串转换与内存分配函数
原文链接:http://www.orlion.ga/977/ 一.数值字符串转换函数 #include <stdlib.h> int atoi(const char *nptr); dou ...
- 《挑战30天C++入门极限》C++的iostream标准库介绍(3)
C++的iostream标准库介绍(3) C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出. 1.有流对象的成员函 ...
- 《挑战30天C++入门极限》C++的iostream标准库介绍(2)
C++的iostream标准库介绍(2) 接下来我们继续看一下C++风格的串流控制,C++引入了ostringstream.istringstream.stringstream这三个类,要使用 ...
- 《挑战30天C++入门极限》C++的iostream标准库介绍(1)
C++的iostream标准库介绍(1) 我们从一开始就一直在利用C++的输入输出在做着各种练习,输入输出是由iostream库提供的,所以讨论此标准库是有必要的,它与C语言的stdio库不同 ...
- 用robotframework 标准库String解决由于存在千分位分隔符导致两个数值不相等的问题。
在编写robotframework自动化断言的过程中,我遇到了如下问题: 我想写一个两个金额判断是否相等的断言,其中一个金额是展示字段存在千分位分隔符,另一个金额是input带入字段,没有千分位分隔符 ...
- 9.Python3标准库--数据压缩与归档
''' 尽管现代计算机系统的存储能力日益增长,但生成数据的增长是永无休止的. 无损(lossless)压缩算法以压缩或解压缩数据花费的时间来换取存储数据所需要的空间,以弥补存储能力的不足. Pytho ...
- 6.Python3标准库--数学运算
''' 作为一种通用的变成语言,Python经常用来解决数学问题.它包含一些用于管理整数和浮点数的内置类型,这很适合完成一般应用中可能出现的基本数学运算. 而标准库中包含一些用于满足更高级需求的模块. ...
- python常用标准库(时间模块 time和datetime)
常用的标准库 time时间模块 import time time -- 获取本地时间戳 时间戳又被称之为是Unix时间戳,原本是在Unix系统中的计时工具. 它的含义是从1970年1月1日(UTC/G ...
随机推荐
- SQL Server阻止了对组件xp_cmdshell过程的解决方案 分类: SQL Server 2015-03-05 08:31 305人阅读 评论(0) 收藏
SQL Server阻止了对组件xp_cmdshell过程的解决方案 错误描述:SQL Server阻止了对组件'xp_cmdshell'的过程'sys.xp_cmdshell'的访问.因为此组件已作 ...
- IOS开发中 RunLoop,RunTime
1.Objective-C中的函数调用 对于C语言,函数调用是由编译器直接转化完成的,在编译时程序就开始查找要执行的函数(C语言函数调用原理).而在OC中,我们将函数调用称为消息发送.在编译时程序不查 ...
- Media Queries 自适应布局展示
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 教你Mac OS系统四种改动Hosts文件的方法
使用Mac OS X系统的用户.在某些时候可能遇到了须要改动系统Hosts文件的情况,那么Mac OS系统怎样改动Hosts文件呢?和Windows系统有何差别呢?我们知道事实上改动Hosts文件仅仅 ...
- Android GridView 一行显示数据(包括图片和文本),解决的办法是计算数据占该行的宽度是多少
最近在做图片的浏览功能,开始是使用Gallery做,但是,达不到我想要的效果,关于使用Gallery显示缩略图的缺点和优点,不在详述了.以下是一个完整的Demo代码,注意我的模拟器是640*960. ...
- Qt 学习之路:坐标系统
在经历过实际操作,以及前面一节中我们见到的那个translate()函数之后,我们可以详细了解下 Qt 的坐标系统了.泛泛而谈坐标系统,有时候会觉得枯燥无味,难以理解,好在现在我们已经有了基础. 坐标 ...
- UITableView beginUpdate和endUpdate使用的前提
转载地址:http://blog.csdn.net/vieri_ch/article/details/46893023 UITableView有两个方法,用于单元格动画变化的方法,beginUpdat ...
- mysql 操作指令笔记
设置区分大小写: 打开my.ini,最后加入: [mysqld] lower_case_table_names=2 (2表示区分大小写,但仅限于字段,数据库名.表名.存储过程名都是小写的) 查看方法: ...
- asp.net 事件模型
asp.net的原始设计构想,就是要让开发人员能够像 VB 开发工具那样,可以使用事件驱动式程序开发模式 (Event-Driven Programming Model) 的方法来开发网页与应用程序, ...
- C#,MVC视图中把枚举转成DropdownList
1.拓展EnumHelper public static class EnumHelper { // Get the value of the description attribute if the ...