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 ...
随机推荐
- 【Android - 框架】之RxJava的使用
RxJava算是最新最常用的,也是程序员们最喜欢的框架之一了. RxJava的核心由Observable(被观察者,事件源)和Subscriber(观察者)构成,Observable负责发出一系列事件 ...
- 用的最多的Android Studio 快捷键
1.Ctrl+D,Ctrl+C 复制删除整一行 2.Ctrl+Alt+L 格式化代码 看起来更好看 3.Ctrl+Q 查看函数API定义 4.Atl+方向键 切换不同文档 平时用快捷键能够提高效率,少 ...
- [AngularJS + Unit Testing] Testing Directive's controller with bindToController, controllerAs and isolate scope
<div> <h2>{{vm.userInfo.number}} - {{vm.userInfo.name}}</h2> </div> 'use str ...
- header的用法小结(转)
php header()函数的具体作用是向客户端发送一个原始 HTTP 标头[Http Header]到客户端. 标头 (header) 是服务器以 HTTP 协义传 HTML 资料到浏览器前所送出的 ...
- 初步掌握MapReduce的架构及原理
目录 1.MapReduce定义 2.MapReduce来源 3.MapReduce特点 4.MapReduce实例 5.MapReduce编程模型 6.MapReduce 内部逻辑 7.MapRed ...
- 如何为 setTimeout() 方法传参
现有如下JavaScript代码: function printApple(apple){ console.log(apple, "is a kind of healthy fruit&qu ...
- 怎样写好一份IT技术岗位的简历
10月是校园招聘的旺季,很多应届毕业生都忙碌起来了,从CSDN笔试-面试文章的火热程度,从我收到的简历就看得出来. 我很久没有参与笔试和面试了,所以只能从“简历”来阐述下我的看法. 截至目前,已经帮8 ...
- 10.11 noip模拟试题
4题均为128M,1s 1. 锻炼计划(exercise.pas) 身体是革命的本钱,OIers不要因为紧张的学习和整天在电脑前而忽视了健康问题.小x设计了自己的锻炼计划,但他不知道这个计划是否可行, ...
- css(动画,过渡,转换)
css3动画 @keyframes 规定动画,必须定义动画的名称,动画时长的百分比,一个或多个css样式属性 以百分比来规定改变发生的时间,或者通过关键词"from"和" ...
- php验证是否为手机端还是PC
<?php $forasp = strtolower($_SERVER['HTTP_USER_AGENT']); if(strpos($forasp,'mobile')==true) { ech ...