std::numeric_limits是C/C++11中的一个模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统C语言,所采用的预处理常数。比较常用的使用是对于给定的基础类型用来判断在当前系统上的最大值、最小值。下面通过一段程序看看std::numeric_limits是怎么使用的。

#include <iostream>
#include <cstdlib>
#include <limits>int main()
{
std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << std::endl;
std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << std::endl;
std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << std::endl;
std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << std::endl;
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << std::endl;std::cout << "Minimum value for float: " << std::numeric_limits<float>::min() << std::endl; // min returns the smallest positive value the type can encode, not the lowest
std::cout << "Lowest value for float: " << std::numeric_limits<float>::lowest() << std::endl; // the lowest value
std::cout << "Maximum value for float: " << std::numeric_limits<float>::max() << std::endl;
std::cout << "float is signed: " << std::numeric_limits<float>::is_signed << std::endl;
std::cout << "Non-sign bits in float: " << std::numeric_limits<float>::digits << std::endl;
std::cout << "float has infinity: " << std::numeric_limits<float>::has_infinity << std::endl;std::cout << "Minimum value for unsigned short: " << std::numeric_limits<unsigned short>::min() << std::endl;
std::cout << "Maximum value for unsigned short: " << std::numeric_limits<unsigned short>::max() << std::endl;std::cout << "is_specialized(float): " << std::numeric_limits<float>::is_specialized << std::endl;
std::cout << "is_integer(float): " << std::numeric_limits<float>::is_integer << std::endl;
std::cout << "is_exact(float): " << std::numeric_limits<float>::is_exact << std::endl;
std::cout << "is_bounded(float): " << std::numeric_limits<float>::is_bounded << std::endl;
std::cout << "is_modulo(float): " << std::numeric_limits<float>::is_modulo << std::endl;
std::cout << "is_iec559(float): " << std::numeric_limits<float>::is_iec559 << std::endl;
std::cout << "digits10(float): " << std::numeric_limits<float>::digits10 << std::endl;
std::cout << "radix(float): " << std::numeric_limits<float>::radix << std::endl;
std::cout << "min_exponent(float): " << std::numeric_limits<float>::min_exponent << std::endl;
std::cout << "max_exponent(float): " << std::numeric_limits<float>::max_exponent << std::endl;
std::cout << "min_exponent10(float): " << std::numeric_limits<float>::min_exponent10 << std::endl;
std::cout << "max_exponent10(float): " << std::numeric_limits<float>::max_exponent10 << std::endl;
std::cout << "epsilon(float): " << std::numeric_limits<float>::epsilon() << std::endl;
std::cout << "round_style(float): " << std::numeric_limits<float>::round_style << std::endl;std::cout << "The smallest nonzero denormalized value for float: "<< std::numeric_limits<float>::denorm_min()<< std::endl;
std::cout << "The difference between 1 and the smallest value greater than 1 for float: "<< std::numeric_limits<float>::epsilon()<< std::endl;
std::cout << "Whether float objects allow denormalized values: "<< std::numeric_limits<float>::has_denorm << std::endl;
std::cout << "Whether float objects can detect denormalized loss: "<< std::numeric_limits<float>::has_denorm_loss << std::endl;
std::cout << "Whether float objects have quiet_NaN: "<< std::numeric_limits<float>::has_quiet_NaN << std::endl;
std::cout << "Whether float objects have a signaling_NaN: "<< std::numeric_limits<float>::has_signaling_NaN << std::endl;
std::cout << "The base for type float is:  "<< std::numeric_limits<float>::radix << std::endl;
std::cout << "The maximum rounding error for type float is:  "<< std::numeric_limits<float>::round_error() << std::endl;
std::cout << "The rounding style for a double type is: "<< std::numeric_limits<double>::round_style << std::endl;
std::cout << "The signaling NaN for type float is:  "<< std::numeric_limits<float>::signaling_NaN() << std::endl;
std::cout << "Whether float types can detect tinyness before rounding: "<< std::numeric_limits<float>::tinyness_before << std::endl;
std::cout << "Whether float types have implemented trapping: "<< std::numeric_limits<float>::traps << std::endl;return 0;
}

利用 g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.71.0/gcc-head/include -std=c++11 的执行结果如下:

Minimum value for int: -2147483648
Maximum value for int: 2147483647
int is signed: true
Non-sign bits in int: 31
int has infinity: false
Minimum value for float: 1.17549e-38
Lowest value for float: -3.40282e+38
Maximum value for float: 3.40282e+38
float is signed: true
Non-sign bits in float: 24
float has infinity: true
Minimum value for unsigned short: 0
Maximum value for unsigned short: 65535
is_specialized(float): true
is_integer(float): false
is_exact(float): false
is_bounded(float): true
is_modulo(float): false
is_iec559(float): true
digits10(float): 6
radix(float): 2
min_exponent(float): -125
max_exponent(float): 128
min_exponent10(float): -37
max_exponent10(float): 38
epsilon(float): 1.19209e-07
round_style(float): 1
The smallest nonzero denormalized value for float: 1.4013e-45
The difference between 1 and the smallest value greater than 1 for float: 1.19209e-07
Whether float objects allow denormalized values: 1
Whether float objects can detect denormalized loss: false
Whether float objects have quiet_NaN: true
Whether float objects have a signaling_NaN: true
The base for type float is:  2
The maximum rounding error for type float is:  0.5
The rounding style for a double type is: 1
The signaling NaN for type float is:  nan
Whether float types can detect tinyness before rounding: false
Whether float types have implemented trapping: false

std::numeric_limits的使用相关推荐

  1. boost::multiprecision模块将 std::numeric_limits 用作 multiprecision.qbk 上的多精度文档片段的示例

    boost::multiprecision模块将 std::numeric_limits 用作 multiprecision.qbk 上的多精度文档片段的示例 实现功能 C++实现代码 实现功能 bo ...

  2. C++ std::numeric_limits

    一 简介 头文件<limits> template <class T> numeric_limits; Provides information about the prope ...

  3. C++/C++11中std::numeric_limits的使用

    在C/C++11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统C语言,所采用的预处理常数.比较常用的使用是对于给定的基础类型用来判断在当前系 ...

  4. max std value 宏_【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题...

    多年以前,Microsoft 幹了一件比 #define N 3 還要蠢的蠢事,那就是在 放入了 min/max 這兩個宏命令(macros). #define max(a,b)            ...

  5. C++数值极限numeric_limits

    一般来说,数值类型的极值是一个与平台相关的特性.C++标准程序库通过template numeric_limits提供这些极值,取代传统C语言所采用的预处理常数.你仍然可以使用后者,其中整数常数定义于 ...

  6. std::setprecision、std::ios::fixed使用说明

    单独使用std::setprecision 如下: #include <iostream> #include <iomanip> #include <cmath> ...

  7. std::chrono时间库详解

    主要时间类型 std::ratio<num, den> 定义分式(std::ratio模板请参考<C++新标准之std::ratio>),例如: std::ratio<6 ...

  8. C++语言中std::array的神奇用法总结,你需要知道!

    摘要:在这篇文章里,将从各个角度介绍下std::array的用法,希望能带来一些启发. td::array是在C++11标准中增加的STL容器,它的设计目的是提供与原生数组类似的功能与性能.也正因此, ...

  9. numeric_limits

    numeric_limits <int> 我们常讲,int:是有符号整型,而 unsigned int:是无符号整型,可此话怎讲呢,自然是值域(range),也即数值范围不同. std:: ...

  10. isnan函数返回值c语言,C++ std::isnan等函数的使用

    C和C++11标准提供了类似于isnan.isfinite.isinf.isnormal.fpclassify分别用于判断是非数(NaN)值.有限制.无穷值.正常数值等. 今天在使用Modbus读取设 ...

最新文章

  1. 计算机二级c语言考完多久出成绩,计算机二级C语言的成绩什么时候出来?
  2. php 获取js变量
  3. 自主可控芯片多路服务器,中国国防领域最高水准自主可控多单元服务器亮相
  4. iOS 6 自动布局入门
  5. [转]动态规划DP的分类
  6. 小程序添加和删除新元素功能实例
  7. Linux:libxml2的安装及使用示例(C语言)
  8. ResNet到底在解决一个什么问题呢?
  9. 电脑有独显内存还被占用_内存条:独立显卡显存,正式再见
  10. flex trace无法使用
  11. IO编程,真的很简单
  12. 基于Vision Transformer的图像去雾算法研究与实现(附源码)
  13. linux dmesg命令参数及用法详解
  14. Oracle LiveLabs实验:Manage and Monitor Autonomous Database
  15. python读写文件的语句_用Python读写文件指南
  16. 问渠哪得清如许?为有源头活水来。——AI与传统文化会碰撞出何种火花呢?
  17. VBA的 随机数 rnd 和 randomize 如何配合使用? 伪随机数带来的问题,根据需要产生不同的随机数!
  18. c log 指定服务器,解决 SQL2000安装程序配置服务器失败。参考服务器错误日志和C:\WINDOWS\sqlstp.log方法...
  19. 连接被远程服务器中断,Windows 2008远程桌面3389连接上后会被立即断开,并提示“远程桌面会话已结束”解决方案...
  20. vue2 + elementui 日期时间选择器 禁止选择当前时间之前的日期及时间,并添加相应校验规则

热门文章

  1. everedit选择_EverEdit
  2. gitlab服务: kex_exchange_identification: Connection closed by remote host
  3. 【转载】NP完全问题——最小曼哈顿网络
  4. java.sql.SQLException: Access denied for user '''localhost' (using password: NO) 的处理方法
  5. 麒麟芯片鸿蒙芯片高通骁龙,麒麟和骁龙两款处理器,选哪一款比较好,看完这三点你就明白了...
  6. 创建windows虚拟机详细教程
  7. Java牛客网输入测试用例
  8. 流利阅读12.23 The 'great dying': rapid warming caused largest extinction event ever, report says
  9. Nginx配置虚拟主机
  10. laravel集合collect中的implode