专栏首页上善若水c++_std-numeric_limits极值接口

c++_std-numeric_limits极值接口

std::numeric_limits

在C/C++11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息。

用于取代<climits>和<limits.h>,浮点常数定义于<cfloat>和<float.h>。

新的极值概念有两个优点,

  • 一是提供了更好的类型安全性,
  • 二是程序员可借此写出一些template以核定这些极值。

member

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

测试目标类型是不是可以用整型来表示(比如char是1,float是0

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

返回目标数据类型能表示的最逼近1的正数和1的差的绝对值

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

测试目标类型是不是可以非规范化表示

has_denorm_loss

bool

测试所有类型是不是能测出因为非规范化而造成的精度损失(不是因为结果本身的不精确)

infinity()

T

检查目标类型的无限类型(如果支持无限表示)

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

测试目标类型是不是符合IEC559标准

is_bounded

bool

检查目标类型的取值是否有限

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.

sample code

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n152" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="c++">#include<iostream>

include<string>

include<limits>

​ using namespace std; int main(){ cout<<"numeric_limits<int>::min()= "<<numeric_limits<int>::min()<<endl; cout<<"numeric_limits<int>::max()= "<<numeric_limits<int>::max()<<endl; cout<<"numeric_limits<short>::min()= "<<numeric_limits<short>::min()<<endl; cout<<"numeric_limits<short>::max()= "<<numeric_limits<short>::max()<<endl; cout<<"numeric_limits<double>::min()= "<<numeric_limits<double>::min()<<endl; cout<<"numeric_limits<double>::max()= "<<numeric_limits<double>::max()<<endl; ​ cout<<"numeric_limits<int>::is_signed()= "<<numeric_limits<int>::is_signed<<endl;//是否有正负号 cout<<"numeric_limits<string>::is_specialized()= "<<numeric_limits<string>::is_specialized<< endl;//是否定义了数值极限 system("pause"); return 0; } </pre>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • java003-错误:java.security.InvalidKeyException: Illegal key size or default parameters解决方法

    其实本质是 解决java加密解密的授权签名问题。 Java几乎各种常用加密算法都能找到对应的实现。因为美国的出口限制,Sun通过权限文件(local_poli...

    上善若水.夏
  • 035android初级篇之[转]android的ViewGroup与View

    Android中的View包含了用户交互和显示,类似于Windows操作系统中的window。

    上善若水.夏
  • 013android初级篇之Android Studio 引用源码模块,jar及so文件

    013android初级篇之Android Studio 引用源码模块,jar及so文件

    上善若水.夏
  • 一道小CTF

    lonelyvaf
  • 快速开发基于 HTML5 网络拓扑图应用1

    HT_hightopo
  • 关于正则表达式第四篇(r3笔记第53天)

    正则表达式,林林总总说了几篇,还是有不少的内容需要补充。 -->星号 字符出现0次或者多次 $ echo "ik"|sed -n '/e*/p' ik $ e...

    jeanron100
  • 32和64位的CentOS 6.0下 安装 Mono 2.10.8 和Jexus 5.0

    CentOS是一个基于RHEL的Linux发行版,其目的是为了提供一套免费与自由的企业等级Linux发行版。CentOS的名称来自于「Community ENT...

    张善友
  • Activiti开发案例之工作流任务数据统计

    工作流可以提高企业运营效率、改善企业资源利用、提高企业运作的灵活性和适应性、提高工作效率、集中精力处理核心业务、跟踪业务处理过程、量化考核业务处理的效率、减少浪...

    小柒2012
  • MySQL8.0的反连接

    在MySQL 8.0.17中,我们在TPC-H基准测试中观察到一个特定的查询。该查询的执行速度比MySQL 8.0.16快20%。这项改进的原因是实施了“ an...

    MySQLSE
  • scala转换date提取年月日时分秒

    从数据库提取出来的时间为 String 格式,现在需要转换为 date 并提取出里面的 小时 时间段:

    机器学习和大数据挖掘

扫码关注云+社区

领取腾讯云代金券