http://blog.csdn.net/netrookie/article/details/5530578

初学C++的时候,对这个模板很陌生,不知道它到底是做什么用的,今天拿起《C++标准程序库》,出现了它的讨论,所以决定好好研究一番。

1. numeric_limits是什么?

(A)《C++标准程序库》:

view plain
  1. 一般来说,数值型别的极值是一个与平台相关的特性。C++标准程序库通过template numeric_limits提供这些极值,取代传统C语言,所采用的预处理常数。新的极值概念有两个优点,第一是提供更好的型别安全性,第二是程序员可借此写出一些template以核定这些极值。

(B)MSDN

view plain
  1. The template class describes arithmetic properties of built-in numerical types.
  2. The header defines explicit specializations for the types wchar_t, bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double, and long double. For these explicit specializations, the member numeric_limits::is_specialized is true, and all relevant members have meaningful values. The program can supply additional explicit specializations. Most member functions of the class describe or test possible implementations of float.
  3. For an arbitrary specialization, no members have meaningful values. A member object that does not have a meaningful value stores zero (or false) and a member function that does not return a meaningful value returns Type(0).
  4. 上面的意思是说:
  5. 这个模板类描述了内建类型的数值属性。
  6. C++标准库显式地为wchar_t, bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double, and long double这些类型提供了特化。对于这些类型来说,is_specialized为true,并且所有的相关的成员(成员变量或成员函数)是有意义的。这个模板也提供其他的特化。大部分的成员函数可以用float型别来描述或测试。
  7. 对于一个任意的特化,相关的成员是没有意义的。一个没有意义的对象一般用0(或者false)来表示,一个没有意义的成员函数会返回0.

(C)我的理解

view plain
  1. 说白了,它是一个模板类,它主要是把C++当中的一些内建型别进行了封装,比如说numeric_limits<int>是一个特化后的类,从这个类的成员变量与成员函数中,我们可以了解到int的很多特性:可以表示的最大值,最小值,是否是精确的,是否是有符号等等。如果用其他任意(非内建类型)来特化这个模板类,比如string,string怎么可能有最大值?我们从MSDN上可以了解到,这对string,成员变量与成员函数是没有意义的,要么返回0要么为false。

2. 小例展示numeric_limits的基本用法:

view plain
  1. #include <limits>
  2. #include <iostream>
  3. using namespace std;
  4. int main() {
  5. cout << boolalpha;
  6. cout << "max(short): " << numeric_limits<short>::max() << endl;
  7. cout << "min(short): " << numeric_limits<short>::min() << endl;
  8. cout << "max(int): " << numeric_limits<int>::max() << endl;
  9. cout << "min(int): " << numeric_limits<int>::min() << endl;
  10. cout << "max(long): " << numeric_limits<long>::max() << endl;
  11. cout << "min(long): " << numeric_limits<long>::min() << endl;
  12. cout << endl;
  13. cout << "max(float): " << numeric_limits<float>::max() << endl;
  14. cout << "min(float): " << numeric_limits<float>::min() << endl;
  15. cout << "max(double): " << numeric_limits<double>::max() << endl;
  16. cout << "min(double): " << numeric_limits<double>::min() << endl;
  17. cout << "max(long double): " << numeric_limits<long double>::max() << endl;
  18. cout << "min(long double): " << numeric_limits<long double>::min() << endl;
  19. cout << endl;
  20. cout << "is_signed(char): "
  21. << numeric_limits<char>::is_signed << endl;
  22. cout << "is_specialized(string): "
  23. << numeric_limits<string>::is_specialized << endl;
  24. }

我机器上的运行结果:

view plain
  1. max(short): 32767
  2. min(short): -32768
  3. max(int): 2147483647
  4. min(int): -2147483648
  5. max(long): 2147483647
  6. min(long): -2147483648
  7. max(float): 3.40282e+038
  8. min(float): 1.17549e-038
  9. max(double): 1.79769e+308
  10. min(double): 2.22507e-308
  11. max(long double): 1.79769e+308
  12. min(long double): 2.22507e-308
  13. is_signed(char): true
  14. is_specialized(string): false
  15. 请按任意键继续. . .

关于为什么float的最小值竟然是正的?我也存在疑问,从结果中,我们看出,min返回的是float型别可以表示的最小的正值,

而不是最小的float数。

从这个例子中,我们差不多了解到numeric_limits的基本用法。

3. 基本成员函数

我以float类型来展示:

view plain
  1. #include <limits>
  2. #include <iostream>
  3. using namespace std;
  4. int main() {
  5. cout << boolalpha;
  6. // 可以表示的最大值
  7. cout << "max(float): " << numeric_limits<float>::max() << endl;
  8. // 可以表示的大于0的最小值,其他类型的实现或与此不同
  9. cout << "min(float): " << numeric_limits<float>::min() << endl;
  10. // 标准库是否为其实现了特化
  11. cout << "is_specialized(float): " << numeric_limits<float>::is_specialized << endl;
  12. // 是否是有符号的,即可以表示正负值
  13. cout << "is_signed(float): " << numeric_limits<float>::is_signed << endl;
  14. // 不否是整形的
  15. cout << "is_integer(float): " << numeric_limits<float>::is_integer << endl;
  16. // 是否是精确表示的
  17. cout << "is_exact(float): " << numeric_limits<float>::is_exact << endl;
  18. // 是否存在大小界限
  19. cout << "is_bounded(float): " << numeric_limits<float>::is_bounded << endl;
  20. // 两个比较大的数相加而不会溢出,生成一个较小的值
  21. cout << "is_modulo(float): " << numeric_limits<float>::is_modulo << endl;
  22. // 是否符合某某标准
  23. cout << "is_iec559(float): " << numeric_limits<float>::is_iec559 << endl;
  24. // 不加+-号可以表示的位数
  25. cout << "digits(float): " << numeric_limits<float>::digits << endl;
  26. // 十进制数的个数
  27. cout << "digits10(float): " << numeric_limits<float>::digits10 << endl;
  28. // 一般基数为2
  29. cout << "radix(float): " << numeric_limits<float>::radix << endl;
  30. // 以2为基数的最小指数
  31. cout << "min_exponent(float): " << numeric_limits<float>::min_exponent << endl;
  32. // 以2为基数的最大指数
  33. cout << "max_exponent(float): " << numeric_limits<float>::max_exponent << endl;
  34. // 以10为基数的最小指数
  35. cout << "min_exponent10(float): " << numeric_limits<float>::min_exponent10 << endl;
  36. // 以10为基数的最大指数
  37. cout << "max_exponent10(float): " << numeric_limits<float>::max_exponent10 << endl;
  38. // 1值和最接近1值的差距
  39. cout << "epsilon(float): " << numeric_limits<float>::epsilon() << endl;
  40. // 舍入方式
  41. cout << "round_style(float): " << numeric_limits<float>::round_style << endl;
  42. }

运行结果:

view plain
  1. max(float): 3.40282e+038
  2. min(float): 1.17549e-038
  3. is_specialized(float): true
  4. is_signed(float): true
  5. is_integer(float): false
  6. is_exact(float): false
  7. is_bounded(float): true
  8. is_modulo(float): false
  9. is_iec559(float): true
  10. digits(float): 24
  11. digits10(float): 6
  12. radix(float): 2
  13. min_exponent(float): -125
  14. max_exponent(float): 128
  15. min_exponent10(float): -37
  16. max_exponent10(float): 38
  17. epsilon(float): 1.19209e-007
  18. round_style(float): 1
  19. 请按任意键继续. . .

还有一些我自己也没有开清楚,以后再补充吧……

【C++】limits头文件 numeric_limits相关推荐

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

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

  2. C++ limits头文件

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

  3. 02头文件的冲突导致,清除缓冲区失败之cin.ignore() 问题

    输入任意多个整数, 把这些数据保存到文件data.txt中. 如果在输入的过程中, 输入错误, 则提示用户重新输入. 指导用户输入结束(按ctrl + z) [每行最多保存4个整数] 可能遇到的 ci ...

  4. C++基础::函数、类、类型所在的头文件 接口的介绍

    除非特别说明,所在的命名空间均是:标准命名空间,也即std: stuff header 说明 ifstream ofstream fstream <fstream> 文件流 in>& ...

  5. gmapping算法教程(5)------scanmatcher和gridfastslam头文件

    scanmatcher和gridfastslam头文件 1.gridlinetraversal.h 2.scanmatcher.h 3.gridslamprocessor.h 4.NEXT 今天,将讲 ...

  6. C++11中头文件chrono的使用

    在C++11中,<chrono>是标准模板库中与时间有关的头文件.该头文件中所有函数与类模板均定义在std::chrono命名空间中. std::chrono是在C++11中引入的,是一个 ...

  7. 万能头文件#include<bits/stdc++.h>更新GCC10.2.0版本

    C++标准库里的万能头文件:#include<bits/stdc++.h> 可用于各大Online Judge测试平台(POJ除外,这些年不维护更新了) 由于网上的都是2014年版的万能头 ...

  8. C++预编译头文件 bits/stdc++.h

    有时候会看到别人包含这样的头文件: #include "bits/stdc++.h" 这个头文件中有很多预先包含的头文件,内容如下: // C++ includes used fo ...

  9. linux c 各头文件作用总结

    #include <linux/***.h> 是在linux-2.6.29/include/linux下面寻找源文件. #include <asm/***.h> 是在linux ...

最新文章

  1. python连接mysql很慢 2.7_Python 2.7 学习笔记 访问mysql数据库
  2. 免费在线阅读:用于计算机视觉、机器学习、机器人的线性代数丨资源
  3. libevent中的时间及相关的管理
  4. 《Effective Java》读书笔记 Item 1:考虑静态工厂方法,而不是构造器
  5. ETL异构数据源Datax_使用数据分片提升同步速度_05
  6. win10卓越性能模式,提升电脑性能
  7. Java实例---flappy-bird实例[最终版]
  8. 百度地图Polyline 清除
  9. java web 程序---javaBean
  10. 电脑中病毒了一直下载安装软件怎么办?
  11. 极大似然法python例子
  12. Linux中rpm详解
  13. 前端vue生成二维码
  14. 马哥教育SRE笔记【作业】week02
  15. 什么是OBD及组成和作用、工作原理
  16. Ubuntu20.04的一些功能设置记录(持续更新)
  17. 股权的秘密:如何把握公司的控制权
  18. python股票预测的意义是什么_股票预测python,python 设计一个名为Stock的类来表示...
  19. 【037】PhotoMosh–艺术故障图片在线生成器
  20. 高通滤波与低通滤波的简单理解

热门文章

  1. C语言 —— 你不得不知道的 scanf 的高级用法
  2. 1414,成绩(C++一本通评测系统)
  3. NodeJS简介-node.js是什么?
  4. Centos7安装Promethus(普罗米修斯)监控系统完整版
  5. HTML 网页打印实现分页打印功能
  6. udf,udaf,udtf之间的区别
  7. android手电筒功能吗,android通过led实现手电筒功能
  8. 微信小程序-组件的生命周期
  9. 小地鼠偷吃萝卜(模板)
  10. mysql查询名字叫小明的_MySQL(命令和查询语句)