包含头文件:#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;
}

最新文章

  1. 网友分享 调用dll的语音朗读 不能变速,不好
  2. 视觉机器学习读书笔记--------BP学习
  3. linux 查看所有用户
  4. Quicksum -SilverN
  5. [MacOS] xcrun: error: active developer path (&quot;/Volumes/Xcode/Xcode6-Beta.app/Contents/Developer&quot;) does not exist, use xcode-select to change
  6. Axis2/c 知识点
  7. POJ 1611
  8. pomelo windows 安装笔记
  9. easyui plugin —— etreegrid:CRUD Treegrid
  10. read op case $op in
  11. resharper 设置代码颜色
  12. 一个想法(续六):IT联盟创业计划:如何进行找钱、寻人、做事?
  13. 图片流量节省大杀器:基于腾讯云CDN的sharpP自适应图片技术实践
  14. c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件
  15. SQL SERVER 数据库级联删除
  16. i++是否原子操作?并解释为什么?
  17. AndroidStudio R 文件标红
  18. Apache shiro集群实现 (一) shiro入门介绍
  19. VS背景设置
  20. ruby-super用法

热门文章

  1. OCCI处理CHAR类型字符串变量的不同
  2. Codeforces Beta Round #10 D. LCIS(DP&amp;amp;LCIS)
  3. [转] react-native 之布局篇
  4. Java的演变过程
  5. Java基础知识强化之集合框架笔记44:Set集合之TreeSet保证元素唯一性和自然排序的原理和图解
  6. 关于echarts绘图,主题的更换
  7. [转]C#中yield用法
  8. CentOS LNMP安装phpMyAdmin
  9. poj 3565 ants
  10. 怎样使用svn开发项目