Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >c++_std-numeric_limits极值接口

c++_std-numeric_limits极值接口

作者头像
上善若水.夏
发布于 2019-08-26 09:35:52
发布于 2019-08-26 09:35:52
1.3K0
举报
文章被收录于专栏:上善若水上善若水

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>

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.08.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
C++核心准则ES.103​:防止溢出​
Overflow usually makes your numeric algorithm meaningless. Incrementing a value beyond a maximum value can lead to memory corruption and undefined behavior.
面向对象思考
2020/06/17
3740
C++ 动态新闻推送 第7期
从reddit/hackernews/lobsters/meetingcpp摘抄一些c++动态。
王很水
2021/08/31
4760
Data Representation - Floating Point Numbers
In the last episode we talked about the data representation of integer, a kind of fixed-point numbers. Today we’re going to learn about floating-point numbers.
零式的天空
2022/03/28
4130
Java学习笔记——Java常用类
String是一个final类,代表不可变的字符序列,底层使用char[]存放。一个字符串对象一旦被配置,其内容是不可变的。
梦飞
2022/06/23
4380
常用的数学函数以及浮点数处理函数
在编程中我们总要进行一些数学运算以及数字处理,尤其是浮点数的运算和处理,这篇文章主要介绍C语言下的数学库。而其他语言中的数学库函数的定义以及最终实现也是通过对C数学库的调用来完成的,其内容大同小异,因此就不在这里介绍了。 C语言标准库中的math.h定义了非常多的数学运算和数字处理函数。这些函数大部分都是在C89标准中定义的,而有些C99标准下的函数我会特殊的说明,同时因为不同的编译器下的C标准库中有些函数的定义有差别,我也会分别的说明。
欧阳大哥2013
2018/08/22
2.6K0
常用的数学函数以及浮点数处理函数
c标准库总结
学习c语言十几年了,却从来没有完整的将c标准库看一看,我想在这一点上我是欠缺的。作为一个技术人员,无论什么时候都不能忘记自己最擅长的技能,这次借一个偶然的契机,翻一翻c标准库,希望以后自己在技术上越来越牛。
用户7886150
2021/02/25
1.5K0
C++ std::isnan等函数的使用
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
ccf19881030
2019/10/22
7.1K0
C++| 编译器预定义的宏
clang -dM -E -x c /dev/null ➜ ~ clang -dM -E -x c /dev/null #define _LP64 1 #define __APPLE_CC__ 6000 #define __APPLE__ 1 #define __ATOMIC_ACQUIRE 2 #define __ATOMIC_ACQ_REL 4 #define __ATOMIC_CONSUME 1 #define __ATOMIC_RELAXED 0 #define __ATOMIC_RELEA
heidsoft
2022/04/18
2.2K0
C++| 编译器预定义的宏
C++(STL):06---数值的极值(numeric_limits类)
一、数值的极值概述 数值类型有着与平台相依的极值 C++标准规定了各种类型必须保证的最小精度。这些最小值如下图所示: 类型 最小长度 char 1byte(8bits) shortint 2bytes int 2bytes longint 4bytes longlongint 8bytes float 4bytes double 8bytes longdouble 8bytes 二、numeric_limits 传统C语言使用预处理器常量来决定数值的极值,其中整数常量定义于<climits>或<li
用户3479834
2021/02/03
1.1K0
C++(STL):06---数值的极值(numeric_limits类)
谁是代码界3%的王者?- 第四题BigDecimal问题简单解读
提到“在Java代码界,有些陷阱外表看起来是个青铜实际上是王者,据说97%工程师会被“秒杀””
明明如月学长
2021/08/31
3420
谁是代码界3%的王者?- 第四题BigDecimal问题简单解读
浮点,多少老司机的血泪史
浮点值应该是我们比较熟悉的一种数据类型,工作中经常用到,会进行比较、计算、转换等等,这些数值操作往往隐藏着很多陷阱,有的可能对计算值产生微小偏差而被忽略,有的可能造成重大软件事故。
用户6879030
2024/06/05
1730
浮点,多少老司机的血泪史
C11 标准特性研究
C11标准是C语言标准的第三版(2011年由ISO/IEC发布),前一个标准版本是C99标准。
用户7886150
2021/02/17
1.2K0
C++ 中文周刊 2024-11-03 第171期
编译器信息最新动态推荐关注hellogcc公众号 本周更新2024-10-30 第278期
王很水
2024/11/04
540
C++ 中文周刊 2024-11-03 第171期
通俗易懂C++:std::optional
如果调用者传入一个语义上无效的值(例如 y = 0 ),此函数无法计算一个返回值(因为除以 0 在数学上是未定义的)。在这种情况下我们该怎么办?
堆栈哲学
2025/03/07
881
通俗易懂C++:std::optional
Go 1.21 新增内置函数:min、max、clear
内置函数 max 返回固定数量的 [cmp.Ordered] 类型参数中的最大值。该接口要求至少提供有一个参数。如果 T 是浮点类型,或有任意一个参数是 NaN,那max 将返回 NaN。
孟斯特
2023/10/19
3260
Go 1.21 新增内置函数:min、max、clear
The Note based on Data Structures and Algorithm Analysis in C CHAPTER 3 P1
The Note based on Data Structures and Algorithm Analysis in C
Tia
2020/09/28
5530
IEEE 754标准--维基百科
IEEE二进制浮点数算术标准(IEEE 754) 是20世纪80年代以来最广泛使用的浮点数运算标准,为许多CPU与浮点运算器所采用。这个标准定义了表示浮点数的格式(包括负零-0)与反常值(denormal number),一些特殊数值((无穷(Inf)与非数值(NaN)),以及这些数值的“浮点数运算符”;它也指明了四种数值舍入规则和五种例外状况(包括例外发生的时机与处理方式)。
风骨散人Chiam
2021/09/06
1.7K0
Data Representation - Integer
Integers, or whole number from elemental mathematics, are the most common and fundamental numbers used in the computers. It’s represented as fixed-point numbers, contrast to floating-point numbers in the machine. Today we are going to learn a whole bunch of way to encode it.
零式的天空
2022/03/28
3690
DECIMAL 数据处理原理浅析
MySQL 中当使用 SELECT 查询常数时,例如:SELECT 123456789.123; 是如何处理的?
GreatSQL社区
2023/02/23
6050
原来C++变量在内存中不是紧密排列的,聊聊内存对齐
今天我们来聊聊C++当中的内存占用,简单回顾一下C++当中的基本变量类型,以及它们分别占用的内存,从而方便我们对程序中使用的变量占用的内存有一个大概的认知。
TechFlow-承志
2022/12/22
1.4K0
原来C++变量在内存中不是紧密排列的,聊聊内存对齐
相关推荐
C++核心准则ES.103​:防止溢出​
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档