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 ...
随机推荐
- sencha touch 框架的几种核心脚本文件之间的区别
sencha-touch-debug.js 供开发时用sencha-touch.js 供发布时用sencha-touch-all.js All 供不能使用SDK构建应用程序时,将 ...
- shell 验证ip
#!/bin/bash function isIp(){ IP=$ ];then echo "Wrong IP!" exit else a=`echo $IP | awk -F . ...
- CHAR 详解
CHAR(20):20指的是表中的a字段能存储的最大字符个数 CREATE TABLE `a` ( `a` char(20) DEFAULT NULL) ENGINE=InnoDB DEFAULT C ...
- Qt 学习之路:绘制设备
绘图设备是继承QPainterDevice的类.QPaintDevice就是能够进行绘制的类,也就是说,QPainter可以在任何QPaintDevice的子类上进行绘制.现在,Qt 提供了若干这样的 ...
- spring aop 如何切面到mvc 的controller--转载
原文:http://yjian84.iteye.com/blog/1920787 网上搜罗半天,不知道什么原因,看了源码,好像他们说的controller 是不受代理的,也对哈,不知道怎么办,于是在h ...
- linux共享windows资料
linux 只有NTFS格式不能访问,其的都可以.1.用fdisk -l 查看分区表.2.然后用mount -t vfat /mnt/hda1 /dev/hda1 就可以了./mnt/hda1是一普通 ...
- Elasticsearch .Net Client NEST 多条件查询示例
Elasticsearch .Net Client NEST 多条件查询示例 /// <summary> /// 多条件搜索例子 /// </summary> public c ...
- jQuery 插件开发 笔记
JQuery 插件开发: 类级别开发,开发新的全局函数 对象级别开发,给Jquery对象开发新方法 一.类级别开发 -定义全局方法 jQuery.foo = function() { alert('T ...
- Android(java)学习笔记202:Handler消息机制的原理和实现
联合学习 Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系 1. 首先我们通过一个实例案例来引出一个异常: (1)布局文件activity_m ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 二进制编码的个体解码操作 decode.c
种群解码函数 decode_pop 为包装函数, 核心调用函数为 decode_ind , 对每个个体进行解码. /* Routines to decode the population */ ...