http://www.cplusplus.com/reference/limits/numeric_limits/
numeric_limits<double>::max ()
是函数,返回编译器允许的 double 型数 最大值。
类似的 numeric_limits<int>::max ()  返回 编译器允许的 int 型数 最大值。
需包含头文件  #include <limits>limits是STL提供的头文件(包含numeric_limits模板类),limit.h是C语言提供的头文件(包含一些宏定义)
例子:
#include <iostream>
#include <limits>
using namespace std;
main(){
cout << std::numeric_limits<double>::max () << endl;
cout << std::numeric_limits<int>::max () << endl;
}
一般来说,数值类型的极值是一个与平台相关的特性。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.

再来一个小事例:

[cpp] view plaincopy
  1. #include<iostream>
  2. #include<string>
  3. #include<limits>   //头文件
  4. using namespace std;
  5. int main(){
  6. cout<<"numeric_limits<int>::min()= "<<numeric_limits<int>::min()<<endl; //int的最小值
  7. cout<<"numeric_limits<int>::max()= "<<numeric_limits<int>::max()<<endl;  //int的最大值
  8. cout<<"numeric_limits<short>::min()= "<<numeric_limits<short>::min()<<endl;
  9. cout<<"numeric_limits<short>::max()= "<<numeric_limits<short>::max()<<endl;
  10. cout<<"numeric_limits<double>::min()= "<<numeric_limits<double>::min()<<endl;
  11. cout<<"numeric_limits<double>::max()= "<<numeric_limits<double>::max()<<endl;
  12. cout<<"numeric_limits<int>::is_signed()= "<<numeric_limits<int>::is_signed<<endl;//是否有正负号
  13. cout<<"numeric_limits<string>::is_specialized()= "<<numeric_limits<string>::is_specialized<<endl;//是否定义了数值极限
  14. system("pause");
  15. return 0;
  16. }

结果如下:

C++ limits头文件的用法(numeric_limits)相关推荐

  1. C++ limits头文件的用法numeric_limits

    参考链接 Cplus plus 参考链接 numeric_limits<double>::max ()是函数,返回编译器允许的 double 型数 最大值. 类似的 numeric_lim ...

  2. #include<set>头文件的用法

    #include<set>头文件的用法 转载与set的常见用法_hjl_heart的博客-CSDN博客_set用法

  3. Visual C++中 #include stdafx.h 头文件的用法

    今天在做VC++实验时,总是出现莫名其妙的错误.比如说: unexpected end of file whilelooking for precompiled header directive 再比 ...

  4. string头文件函数用法大总结

    C++对应的头文件: #include <cstring> C对应的头文件: #include <string.h> 声明一个字符串变量 1)string s;//声明s字符串 ...

  5. 关于C++中string头文件的用法

    注意: 这里需要声明一点,头文件string和string.h是不同的.下面的代码是string的使用例子,在string头文件下,函数中,是可以采取string str = "abcxyz ...

  6. 【C++】limits头文件 numeric_limits

    http://blog.csdn.net/netrookie/article/details/5530578 初学C++的时候,对这个模板很陌生,不知道它到底是做什么用的,今天拿起<C++标准程 ...

  7. C++ limits头文件

    具体用法参考C++官网 / /numeric_limits example #include <iostream> #include <limits> using namesp ...

  8. 头文件的用法及注意事项

    myfunc.h //声明函数 int myCal(int n1,int n2,char oper); myfunc.c //实现myfunc函数//说明 //1. 函数名 cal //2. 有返回值 ...

  9. C/C++ 头文件的作用和用法

    示例代码编译运行环境:Windows 64bits+VS2017+Debug+Win32. 头文件是 C/C++ 程序不可或缺的组成部分,我们需要了解头文件的作用和相关规范. 1.头文件的作用 C/C ...

  10. cqueue结构pop_c++ stl栈容器stack的pop(),push()等用法介绍及头文件

    C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,--也就是说实现了一个先进后出(FILO)的数据结构. c++ stl栈stack的头文件为: #include c++ ...

最新文章

  1. shiro 解决 跨域(仅端口不同) 登陆 问题
  2. 右侧快速入口滑动时左侧跟着变化
  3. Lambda学习笔记
  4. 那些很重要,但是不常用的技术,websocket
  5. AFNetworking框架下的SSL服务器证书的自定义验证
  6. SCCM 部署操作系统 ,提示权限问题,报错:0xc00000098
  7. 俄罗斯方块是java_俄罗斯方块java
  8. SpringBoot项目中使用CXF发布和调用webservice接口
  9. 五子棋项目结束总结_五子棋比赛活动总结
  10. 热烈欢迎Ubuntu志愿者!
  11. 【python实战】怎么用python自动登录CSDN
  12. mysql安装以及安装navicat并且连接
  13. 针对初创型企业的高新企业认定知识及建议!
  14. 未越狱设备 安装ipa
  15. C++练习:编写程序实现歌唱大赛选手成绩计算功能
  16. TI CC32XX SDA中SimpleLink Academy教程翻译(RTOS部分的基础介绍非常易懂)
  17. 计算机求等级值的计算,2015年计算机一级MS OFFICE等级考试计算题
  18. RACE数据集上相关的研究
  19. android nfc识别身份正_[Android] NFC卡模拟专业版 用手机自带NFC开小区门禁 刷食堂饭卡...
  20. 内核调试:一次多线程调试与KASAN检测实例

热门文章

  1. 入职5年前端工程师针对web前端小白,作出的职业规划建议
  2. 刷票投票的自动运行脚本
  3. 项目管理所有计算公式
  4. 【Unity】Unity 几何知识、弧度、三角函数、向量运算、点乘、叉乘
  5. eSPI自学笔记(一):前言和简介
  6. THinkphp教程网址
  7. css 动效彩旗飘扬
  8. Thinkphp 5.1安装
  9. I don't know her
  10. 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)