C++数值极限numeric_limits
一般来说,数值类型的极值是一个与平台相关的特性。C++标准程序库通过template numeric_limits提供这些极值,取代传统C语言所采用的预处理常数。你仍然可以使用后者,其中整数常数定义于<climits>和<limits.h>,浮点常数定义于<cfloat>和<float.h>,新的极值概念有两个优点,一是提供了更好的类型安全性,二是程序员可借此写出一些template以核定这些极值。
1. 函数介绍
member | type | property |
---|---|---|
is_specialized |
bool
|
true for all arithmetic types (i.e., those for which numeric_limits is specialized).false for all other types.
|
min() | T |
Minimum finite value. For floating types with denormalization (variable number of exponent bits): minimum positive normalized value. Equivalent to CHAR_MIN, SCHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN, FLT_MIN, DBL_MIN, LDBL_MIN or 0 , depending on type.
|
max() | T |
Maximum finite value. Equivalent to CHAR_MAX, SCHAR_MAX, UCHAR_MAX, SHRT_MAX, USHRT_MAX, INT_MAX, UINT_MAX, LONG_MAX, ULONG_MAX, LLONG_MAX, ULLONG_MAX, UINT_LEAST16_MAX, UINT_LEAST32_MAX, FLT_MAX, DBL_MAX or LDBL_MAX, depending on type. |
lowest() | T |
Minimum finite value. (since C++11) For integral types: the same as min(). For floating-point types: implementation-dependent; generally, the negative of max(). |
digits | int |
For integer types: number of non-sign bits (radix base digits) in the representation. For floating types: number of digits (in radix base) in the mantissa (equivalent to FLT_MANT_DIG, DBL_MANT_DIG or LDBL_MANT_DIG). |
digits10 |
int
|
Number of digits (in decimal base) that can be represented without change. Equivalent to FLT_DIG, DBL_DIG or LDBL_DIG for floating types. |
max_digits10 |
int
|
Number of digits (in decimal base) required to ensure that values that differ are always differentiated. |
is_signed |
bool
|
true if type is signed.
|
is_integer |
bool
|
true if type is integer.
|
is_exact |
bool
|
true if type uses exact representations.
|
radix |
int
|
For integer types: base of the representation. For floating types: base of the exponent of the representation (equivalent to FLT_RADIX). |
epsilon() | T |
Machine epsilon (the difference between 1 and the least value greater than 1 that is representable). Equivalent to FLT_EPSILON, DBL_EPSILON or LDBL_EPSILON for floating types. |
round_error() | T | Measure of the maximum rounding error. |
min_exponent |
int
|
Minimum negative integer value such that radix raised to (min_exponent-1) generates a normalized floating-point number.Equivalent to FLT_MIN_EXP, DBL_MIN_EXP or LDBL_MIN_EXP for floating types. |
min_exponent10 |
int
|
Minimum negative integer value such that 10 raised to that power generates a normalized floating-point number. Equivalent to FLT_MIN_10_EXP, DBL_MIN_10_EXP or LDBL_MIN_10_EXP for floating types. |
max_exponent |
int
|
Maximum integer value such that radix raised to (max_exponent-1) generates a representable finite floating-point number.Equivalent to FLT_MAX_EXP, DBL_MAX_EXP or LDBL_MAX_EXP for floating types. |
max_exponent10 |
int
|
Maximum integer value such that 10 raised to that power generates a normalized finite floating-point number. Equivalent to FLT_MAX_10_EXP, DBL_MAX_10_EXP or LDBL_MAX_10_EXP for floating types. |
has_infinity |
bool
|
true if the type has a representation for positive infinity.
|
has_quiet_NaN |
bool
|
true if the type has a representation for a quiet (non-signaling) "Not-a-Number".
|
has_signaling_NaN |
bool
|
true if the type has a representation for a signaling "Not-a-Number".
|
has_denorm | float_denorm_style |
Denormalized values (representations with a variable number of exponent bits). A type may have any of the following enum values: denorm_absent, if it does not allow denormalized values. denorm_present, if it allows denormalized values. denorm_indeterminate, if indeterminate at compile time. |
has_denorm_loss |
bool
|
true if a loss of accuracy is detected as a denormalization loss, rather than an inexact result.
|
infinity() | T | Representation of positive infinity, if available. |
quiet_NaN() | T | Representation of quiet (non-signaling) "Not-a-Number", if available. |
signaling_NaN() | T | Representation of signaling "Not-a-Number", if available. |
denorm_min() | T |
Minimum positive denormalized value. For types not allowing denormalized values: same as min() .
|
is_iec559 |
bool
|
true if the type adheres to IEC-559 / IEEE-754 standard.An IEC-559 type always has has_infinity, has_quiet_NaN and has_signaling_NaN set to true ; And infinity, quiet_NaN and signaling_NaN return some non-zero value.
|
is_bounded |
bool
|
true if the set of values represented by the type is finite.
|
is_modulo |
bool
|
true if the type is modulo. A type is modulo if it is possible to add two positive numbers and have a result that wraps around to a third number that is less.
|
traps |
bool
|
true if trapping is implemented for the type.
|
tinyness_before |
bool
|
true if tinyness is detected before rounding.
|
round_style | float_round_style |
Rounding style. A type may have any of the following enum values: round_toward_zero, if it rounds toward zero. round_to_nearest, if it rounds to the nearest representable value. round_toward_infinity, if it rounds toward infinity. round_toward_neg_infinity, if it rounds toward negative infinity. round_indeterminate, if the rounding style is indeterminable at compile time. |
2. 函数定义
template <class T> class numeric_limits {
public:static constexpr bool is_specialized = false;static constexpr T min() noexcept { return T(); }static constexpr T max() noexcept { return T(); }static constexpr T lowest() noexcept { return T(); }static constexpr int digits = 0;static constexpr int digits10 = 0;static constexpr bool is_signed = false;static constexpr bool is_integer = false;static constexpr bool is_exact = false;static constexpr int radix = 0;static constexpr T epsilon() noexcept { return T(); }static constexpr T round_error() noexcept { return T(); }static constexpr int min_exponent = 0;static constexpr int min_exponent10 = 0;static constexpr int max_exponent = 0;static constexpr int max_exponent10 = 0;static constexpr bool has_infinity = false;static constexpr bool has_quiet_NaN = false;static constexpr bool has_signaling_NaN = false;static constexpr float_denorm_style has_denorm = denorm_absent;static constexpr bool has_denorm_loss = false;static constexpr T infinity() noexcept { return T(); }static constexpr T quiet_NaN() noexcept { return T(); }static constexpr T signaling_NaN() noexcept { return T(); }static constexpr T denorm_min() noexcept { return T(); }static constexpr bool is_iec559 = false;static constexpr bool is_bounded = false;static constexpr bool is_modulo = false;static constexpr bool traps = false;static constexpr bool tinyness_before = false;static constexpr float_round_style round_style = round_toward_zero;
};
3. 示例
// numeric_limits example
#include <iostream> // std::cout
#include <limits> // std::numeric_limitsint main () {std::cout << std::boolalpha;std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';return 0;
}
输出
Minimum value for int: -2147483648
Maximum value for int: 2147483647
int is signed: true
Non-sign bits in int: 31
int has infinity: false
参考文献
- http://www.cplusplus.com/reference/limits/numeric_limits/
- https://blog.csdn.net/yhc166188/article/details/90287807
C++数值极限numeric_limits相关推荐
- C++数值类型极限值的获取
C/C++中基本类型的数值极限值一般来说都是与具体平台有关的,在程序设计的过程中为了写出与平台无关的程序则必须通过合理科学的方法去获取各种类型的极值,常用的获取方法有两种:一种是传统的C语言所采用的预 ...
- C++11语言新特性-《C++标准库(第二版)》读书笔记
文章目录 3.1.1 微小但重要的语法提升 Template 表达式内的空格 nullptr 和std::nullptr_t 3.1.2 以auto 完成自动类型推导 3.1.3 一致性初始化(Uni ...
- c++高级编程学习笔记4
C++运算符重载 运算符重载概述 根据第 1 章的描述,C++中的运算符是一些类似于+.<.*和<<的符号.这些运算符可应用于内建类型,例如 int 和 double,从而实现算术操 ...
- C++高级编程(第3版)_学习记录
<C++高级编程(第3版)> Professional C++, Third Edition [美]Narc Gregoire 著,张永强 译,清华大学出版社,2015.5第1版 文章目录 ...
- 管理员必知:服务器基准测试方法与误区
http://www.itwaka.com/ 在上一篇文章<管理员必知:服务器基准测试六大步骤>中,我们介绍了服务器性能衡量的标准以及进行服务器性能测试所必须的六大步骤.所有的准备工作都做 ...
- matlab r2010a教程,MATLAB教程R2010a(十二五)
第1章 基础准备及入门 1.1 MATLAB的安装和工具包选择 1.2 Desktop操作桌面的启动 1.2.1 MATLAB的启动 1.2.2 Desktop操作桌面简介 1.3 Command W ...
- 常数乘以无穷大等于多少_请教一个数学问题:无穷大乘以无穷小等于多少?
实在受不了了,只好出来说两句.好歹也是数学这一行的,看她被你们 糟蹋成这样实在不忍心. "无穷大量"和"无穷小量"在高等数学中都是趋于特定极限的变量的称呼, 一 ...
- 【c++类和对象——设计一个学生类】
设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,并且可以显示. 解法1: #include<iostream> using namespace std; #include<s ...
- 导线载流量_过路老熊_新浪博客
导线载流量 绝缘导线载流量计算表 截面积mm2 系 数 载流量(AL)A 载流量(CU)A 0.75 9 6.75 10 1 10 10 16.5 1.5 11 16.5 30 2.5 12 ...
最新文章
- 5分钟速通 AI 计算机视觉发展应用
- linux报错 find: missing argument to `-exec'
- 数独高阶技巧入门之四:简单异数链
- 【BZOJ】2655: calc 动态规划+拉格朗日插值
- 最优化学习笔记(十六)——拟牛顿法(2)
- C/C++之大端小端
- elmentui的短信验证界面_[javascript] elementui下login登录页界面和js验证逻辑
- Android开发教程 - 使用Data Binding Android Studio不能正常生成相关类/方法的解决办法...
- kuangbin 莫队专题
- 方法重写和重载的区别
- Win11切换桌面快捷键
- springboot的开发流程
- There was an error checking the latest version of pip
- Graph U-Nets小结
- k8s paas部署
- Latex入门_第3章:文档元素
- 小组件打不开-完美解决
- 欧洲语言学习统一标准C1C2音频,昆明学法语梓润告诉你法语欧标A1A2B1B2C1C2
- Python之路—200行Python代码搞了个打飞机游戏!!
- Linux从安装到实战+学校Linux+瑞吉外卖Linux项目部署