C++数值极限numeric_limits

news/2024/7/7 19:51:07

一般来说,数值类型的极值是一个与平台相关的特性。C++标准程序库通过template numeric_limits提供这些极值,取代传统C语言所采用的预处理常数。你仍然可以使用后者,其中整数常数定义于<climits>和<limits.h>,浮点常数定义于<cfloat>和<float.h>,新的极值概念有两个优点,一是提供了更好的类型安全性,二是程序员可借此写出一些template以核定这些极值。

1. 函数介绍

membertypeproperty
is_specializedbooltrue for all arithmetic types (i.e., those for which numeric_limits is specialized).
false for all other types.
min()TMinimum 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()TMaximum 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()TMinimum finite value. (since C++11)
For integral types: the same as min().
For floating-point types: implementation-dependent; generally, the negative of max().
digitsintFor 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).
digits10intNumber of digits (in decimal base) that can be represented without change.
Equivalent to FLT_DIG, DBL_DIG or LDBL_DIG for floating types.
max_digits10intNumber of digits (in decimal base) required to ensure that values that differ are always differentiated.
is_signedbooltrue if type is signed.
is_integerbooltrue if type is integer.
is_exactbooltrue if type uses exact representations.
radixintFor integer types: base of the representation.
For floating types: base of the exponent of the representation (equivalent to FLT_RADIX).
epsilon()TMachine 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()TMeasure of the maximum rounding error.
min_exponentintMinimum 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_exponent10intMinimum 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_exponentintMaximum 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_exponent10intMaximum 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_infinitybooltrue if the type has a representation for positive infinity.
has_quiet_NaNbooltrue if the type has a representation for a quiet (non-signaling) "Not-a-Number".
has_signaling_NaNbooltrue if the type has a representation for a signaling "Not-a-Number".
has_denormfloat_denorm_styleDenormalized 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_lossbooltrue if a loss of accuracy is detected as a denormalization loss, rather than an inexact result.
infinity()TRepresentation of positive infinity, if available.
quiet_NaN()TRepresentation of quiet (non-signaling) "Not-a-Number", if available.
signaling_NaN()TRepresentation of signaling "Not-a-Number", if available.
denorm_min()TMinimum positive denormalized value.
For types not allowing denormalized values: same as min().
is_iec559booltrue 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_boundedbooltrue if the set of values represented by the type is finite.
is_modulobooltrue 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.
trapsbooltrue if trapping is implemented for the type.
tinyness_beforebooltrue if tinyness is detected before rounding.
round_stylefloat_round_styleRounding 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

http://lihuaxi.xjx100.cn/news/247404.html

相关文章

[异常笔记] spring boot 启动-2018040201

异常 1、编码引发异常 00:59:49.311 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : [] 00:59:49.318 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for re…

2018牛客暑期ACM多校训练营第二场 - A题

A题是道水题……结果没注意到编译器不支持I64d卡了快俩小时…… 还要注意的是k1时不是特例&#xff0c;因为这时候走跟跑还是不一样的…… A run 输入描述&#xff1a; The first line of input contains 2 integers Q and k.Q is the number of queries.(Q<100000,1<k…

最近做手机端,GPS,微信QQ分享总结的问题

Android端 百度地图&#xff1a; 1.libs包中armeabi下liblocSDK4d.so文件丢失&#xff0c;导致百度定位失效。 微信分享&#xff1a; 1.分享App,app的内容&#xff08;图片加描述&#xff09;不能超过32kb &#xff0c;不然无法分享。&#xff08;直接跳至Oncancel事件中&#…

基于Sql Server 2008的分布式数据库的实践(一)

原文 基于Sql Server 2008的分布式数据库的实践&#xff08;一&#xff09; 配置Sql Server 2008&#xff08;Win7&#xff09; 1.打开SQL server2012&#xff0c;使用windows身份登录 2.登录后&#xff0c;右键选择“属性”。左侧选择“安全性”&#xff0c;选中右侧的“SQL S…

利用gevent实现异步执行任务

import gevent def task(pid):gevent.sleep(2)print ("task %s done"%pid)def asynchronous():threads [gevent.spawn(task,i) for i in xrange(5)]gevent.joinall(threads)if __name__ __main__:print ("asynchronous")asynchronous()执行结果&#xff…

变量定义原则

1. 命名规则 变量名即反应变量的含义&#xff1b; 变量名只能是字母&#xff0c;数字和下划线的组合&#xff0c;不建议使用特殊字符&#xff1b;关键字不能用作变量名&#xff1b;使用英文单词或缩写组成变量名&#xff0c;不建议使用汉语拼音或其它语言&#xff1b; 2. 命名习…

脚本中echo显示内容带颜色显示

脚本中echo显示内容带颜色显示,echo显示带颜色&#xff0c;需要使用参数-e格式如下&#xff1a;echo -e "\033[字背景颜色&#xff1b;文字颜色m字符串\033[0m"例如&#xff1a;echo -e "\033[41;36m something here \033[0m"其中41的位置代表底色&#xf…

【贪心】Google Code Jam Round 1A 2018 Waffle Choppers

题意&#xff1a;给你一个矩阵&#xff0c;有些点是黑的&#xff0c;让你横切h刀&#xff0c;纵切v刀&#xff0c;问你是否能让切出的所有子矩阵的黑点数量相等。 设黑点总数为sum&#xff0c;sum必须能整除(h1)&#xff0c;进而sum/(h1)必须能整除(v1)。 先考虑横行&#xff0…