C++标准模板(STL)- 类型支持 (数值极限,max_digits10,radix,min_exponent)

本文主要是介绍C++标准模板(STL)- 类型支持 (数值极限,max_digits10,radix,min_exponent),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

数值极限


定义于头文件 <limits>

定义于头文件 <limits>

template< class T > class numeric_limits;

numeric_limits 类模板提供查询各种算术类型属性的标准化方式(例如 int 类型的最大可能值是 std::numeric_limits<int>::max() )。
 

区别所有此类型值所需的十进制位数

std::numeric_limits<T>::max_digits10

static constexpr int max_digits10

(C++11 起)

 std::numeric_limits<T>::max_digits10 的值是唯一地表示所有类型 T 值的底 10 位数,是为序列化/反序列化到文本所需。此常量对所有浮点类型有意义。

标准特化

Tstd::numeric_limits<T>::max_digits10 的值
/* non-specialized */​0​
bool​0​
char​0​
signed char​0​
unsigned char​0​
wchar_t​0​
char8_t​0​
char16_t​0​
char32_t​0​
short​0​
unsigned short​0​
int​0​
unsigned int​0​
long​0​
unsigned long​0​
long long​0​
unsigned long long​0​
floatFLT_DECIMAL_DIG 或 std::ceil(std::numeric_limits<float>::digits * std::log10(2) + 1)
doubleDBL_DECIMAL_DIG 或 std::ceil(std::numeric_limits<double>::digits * std::log10(2) + 1)
long doubleDECIMAL_DIG 或 LDBL_DECIMAL_DIG 或 std::ceil(std::numeric_limits<long double>::digits * std::log10(2) + 1)

注意

不同于多数数学运算,只要用至少 max_digits10 (对于 float 为 9 ,对于 double 为 17 )位,则从浮点值转换到文本并转换回来是准确的:保证产生同一浮点值,即使不确定的文本表示不准确。以小数点记法,可能需要超过一百个十进制小数位表示 float 的精确值。

调用示例

#include <iostream>
#include <string>
#include <limits>
#include <cstdint>
#include <cfloat>struct SName
{
};//偏特化
struct SPartSpec
{
};namespace std
{
template<>
struct numeric_limits<SPartSpec>
{static _GLIBCXX_USE_CONSTEXPR bool is_specialized   = true;static _GLIBCXX_USE_CONSTEXPR bool is_signed        = true;static _GLIBCXX_USE_CONSTEXPR bool is_integer       = true;static _GLIBCXX_USE_CONSTEXPR bool is_exact         = true;static _GLIBCXX_USE_CONSTEXPR bool has_infinity     = true;static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN    = true;static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm     = denorm_present;static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss  = true;static _GLIBCXX_USE_CONSTEXPR float_round_style round_style     = round_toward_neg_infinity;static _GLIBCXX_USE_CONSTEXPR bool is_iec559        = true;static _GLIBCXX_USE_CONSTEXPR bool is_bounded       = true;static _GLIBCXX_USE_CONSTEXPR bool is_modulo        = true;static _GLIBCXX_USE_CONSTEXPR int  digits           = CHAR_BIT;static _GLIBCXX_USE_CONSTEXPR int  digits10         = CHAR_BIT;static _GLIBCXX_USE_CONSTEXPR int  max_digits10     = DECIMAL_DIG;static _GLIBCXX_USE_CONSTEXPR int  radix            = FLT_RADIX;
};
}int main()
{std::cout << std::boolalpha;std::cout << "std::numeric_limits<bool>::max_digits10:                 "<< std::numeric_limits<bool>::max_digits10 << std::endl;std::cout << "std::numeric_limits<char>::max_digits10:                 "<< std::numeric_limits<char>::max_digits10 << std::endl;std::cout << "std::numeric_limits<signed char>::max_digits10:          "<< std::numeric_limits<signed char>::max_digits10 << std::endl;std::cout << "std::numeric_limits<unsigned char>::max_digits10:        "<< std::numeric_limits<unsigned char>::max_digits10 << std::endl;std::cout << "std::numeric_limits<wchar_t>::max_digits10:              "<< std::numeric_limits<wchar_t>::max_digits10 << std::endl;std::cout << "std::numeric_limits<char16_t>::max_digits10:             "<< std::numeric_limits<char16_t>::max_digits10 << std::endl;std::cout << "std::numeric_limits<char32_t>::max_digits10:             "<< std::numeric_limits<char32_t>::max_digits10 << std::endl;std::cout << "std::numeric_limits<short>::max_digits10:                "<< std::numeric_limits<short>::max_digits10 << std::endl;std::cout << "std::numeric_limits<unsigned short>::max_digits10:       "<< std::numeric_limits<unsigned short>::max_digits10 << std::endl;std::cout << "std::numeric_limits<int>::max_digits10:                  "<< std::numeric_limits<int>::max_digits10 << std::endl;std::cout << "std::numeric_limits<unsigned int>::max_digits10:         "<< std::numeric_limits<unsigned int>::max_digits10 << std::endl;std::cout << "std::numeric_limits<long>::max_digits10:                 "<< std::numeric_limits<long>::max_digits10 << std::endl;std::cout << "std::numeric_limits<unsigned long>::max_digits10:        "<< std::numeric_limits<unsigned long>::max_digits10 << std::endl;std::cout << "std::numeric_limits<long long>::max_digits10:            "<< std::numeric_limits<long long>::max_digits10 << std::endl;std::cout << "std::numeric_limits<unsigned long long>::max_digits10:   "<< std::numeric_limits<unsigned long long>::max_digits10 << std::endl;std::cout << "std::numeric_limits<float>::max_digits10:                "<< std::numeric_limits<float>::max_digits10 << std::endl;std::cout << "std::numeric_limits<double>::max_digits10:               "<< std::numeric_limits<double>::max_digits10 << std::endl;std::cout << "std::numeric_limits<long double>::max_digits10:          "<< std::numeric_limits<long double>::max_digits10 << std::endl;std::cout << "std::numeric_limits<std::string>::max_digits10:          "<< std::numeric_limits<std::string>::max_digits10 << std::endl;std::cout << "std::numeric_limits<SName>::max_digits10:                "<< std::numeric_limits<SName>::max_digits10 << std::endl;std::cout << "std::numeric_limits<SPartSpec>::max_digits10:            "<< std::numeric_limits<SPartSpec>::max_digits10 << std::endl;return 0;
}

输出

给定类型的表示所用的基或整数底

std::numeric_limits<T>::radix

static const int radix;

(C++11 前)

static constexpr int radix;

(C++11 起)

 std::numeric_limits<T>::radix 的值是用于表示该类型的数字系统的底。对于所有二进制数值类型为 2 ,但它可以,譬如对 IEEE 754 十进制浮点类型或第三方二进制编码十进制整数为 10 。此常量对所有特化有意义。

标准特化

Tstd::numeric_limits<T>::radix 的值
/* non-specialized */​0​
bool2
char2
signed char2
unsigned char2
wchar_t2
char8_t2
char16_t2
char32_t2
short2
unsigned short2
int2
unsigned int2
long2
unsigned long2
long long2
unsigned long long2
floatFLT_RADIX
doubleFLT_RADIX
long doubleFLT_RADIX

调用示例

#include <iostream>
#include <string>
#include <limits>
#include <cstdint>
#include <cfloat>struct SName
{
};//偏特化
struct SPartSpec
{
};namespace std
{
template<>
struct numeric_limits<SPartSpec>
{static _GLIBCXX_USE_CONSTEXPR bool is_specialized   = true;static _GLIBCXX_USE_CONSTEXPR bool is_signed        = true;static _GLIBCXX_USE_CONSTEXPR bool is_integer       = true;static _GLIBCXX_USE_CONSTEXPR bool is_exact         = true;static _GLIBCXX_USE_CONSTEXPR bool has_infinity     = true;static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN    = true;static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm     = denorm_present;static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss  = true;static _GLIBCXX_USE_CONSTEXPR float_round_style round_style     = round_toward_neg_infinity;static _GLIBCXX_USE_CONSTEXPR bool is_iec559        = true;static _GLIBCXX_USE_CONSTEXPR bool is_bounded       = true;static _GLIBCXX_USE_CONSTEXPR bool is_modulo        = true;static _GLIBCXX_USE_CONSTEXPR int  digits           = CHAR_BIT;static _GLIBCXX_USE_CONSTEXPR int  digits10         = CHAR_BIT;static _GLIBCXX_USE_CONSTEXPR int  max_digits10     = DECIMAL_DIG;static _GLIBCXX_USE_CONSTEXPR int  radix            = FLT_RADIX;
};
}int main()
{std::cout << std::boolalpha;std::cout << "std::numeric_limits<bool>::radix:                 "<< std::numeric_limits<bool>::radix << std::endl;std::cout << "std::numeric_limits<char>::radix:                 "<< std::numeric_limits<char>::radix << std::endl;std::cout << "std::numeric_limits<signed char>::radix:          "<< std::numeric_limits<signed char>::radix << std::endl;std::cout << "std::numeric_limits<unsigned char>::radix:        "<< std::numeric_limits<unsigned char>::radix << std::endl;std::cout << "std::numeric_limits<wchar_t>::radix:              "<< std::numeric_limits<wchar_t>::radix << std::endl;std::cout << "std::numeric_limits<char16_t>::radix:             "<< std::numeric_limits<char16_t>::radix << std::endl;std::cout << "std::numeric_limits<char32_t>::radix:             "<< std::numeric_limits<char32_t>::radix << std::endl;std::cout << "std::numeric_limits<short>::radix:                "<< std::numeric_limits<short>::radix << std::endl;std::cout << "std::numeric_limits<unsigned short>::radix:       "<< std::numeric_limits<unsigned short>::radix << std::endl;std::cout << "std::numeric_limits<int>::radix:                  "<< std::numeric_limits<int>::radix << std::endl;std::cout << "std::numeric_limits<unsigned int>::radix:         "<< std::numeric_limits<unsigned int>::radix << std::endl;std::cout << "std::numeric_limits<long>::radix:                 "<< std::numeric_limits<long>::radix << std::endl;std::cout << "std::numeric_limits<unsigned long>::radix:        "<< std::numeric_limits<unsigned long>::radix << std::endl;std::cout << "std::numeric_limits<long long>::radix:            "<< std::numeric_limits<long long>::radix << std::endl;std::cout << "std::numeric_limits<unsigned long long>::radix:   "<< std::numeric_limits<unsigned long long>::radix << std::endl;std::cout << "std::numeric_limits<float>::radix:                "<< std::numeric_limits<float>::radix << std::endl;std::cout << "std::numeric_limits<double>::radix:               "<< std::numeric_limits<double>::radix << std::endl;std::cout << "std::numeric_limits<long double>::radix:          "<< std::numeric_limits<long double>::radix << std::endl;std::cout << "std::numeric_limits<std::string>::radix:          "<< std::numeric_limits<std::string>::radix << std::endl;std::cout << "std::numeric_limits<SName>::radix:                "<< std::numeric_limits<SName>::radix << std::endl;std::cout << "std::numeric_limits<SPartSpec>::radix:            "<< std::numeric_limits<SPartSpec>::radix << std::endl;return 0;
}

输出

 底的该数次幂是合法正规浮点值的最小负数加一

std::numeric_limits<T>::min_exponent

static const int min_exponent;

(C++11 前)

static constexpr int min_exponent;

(C++11 起)

标准特化

Tstd::numeric_limits<T>::min_exponent 的值
/* non-specialized */​0​
bool​0​
char​0​
signed char​0​
unsigned char​0​
wchar_t​0​
char8_t​0​
char16_t​0​
char32_t​0​
short​0​
unsigned short​0​
int​0​
unsigned int​0​
long​0​
unsigned long​0​
long long​0​
unsigned long long​0​
floatFLT_MIN_EXP
doubleDBL_MIN_EXP
long doubleLDBL_MIN_EXP

调用示例

#include <iostream>
#include <string>
#include <limits>
#include <cstdint>
#include <cfloat>struct SName
{
};//偏特化
struct SPartSpec
{
};namespace std
{
template<>
struct numeric_limits<SPartSpec>
{static _GLIBCXX_USE_CONSTEXPR bool is_specialized   = true;static _GLIBCXX_USE_CONSTEXPR bool is_signed        = true;static _GLIBCXX_USE_CONSTEXPR bool is_integer       = true;static _GLIBCXX_USE_CONSTEXPR bool is_exact         = true;static _GLIBCXX_USE_CONSTEXPR bool has_infinity     = true;static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN    = true;static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm     = denorm_present;static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss  = true;static _GLIBCXX_USE_CONSTEXPR float_round_style round_style     = round_toward_neg_infinity;static _GLIBCXX_USE_CONSTEXPR bool is_iec559        = true;static _GLIBCXX_USE_CONSTEXPR bool is_bounded       = true;static _GLIBCXX_USE_CONSTEXPR bool is_modulo        = true;static _GLIBCXX_USE_CONSTEXPR int  digits           = CHAR_BIT;static _GLIBCXX_USE_CONSTEXPR int  digits10         = CHAR_BIT;static _GLIBCXX_USE_CONSTEXPR int  max_digits10     = DECIMAL_DIG;static _GLIBCXX_USE_CONSTEXPR int  radix            = FLT_RADIX;static _GLIBCXX_USE_CONSTEXPR int  min_exponent     = FLT_MIN_EXP;
};
}int main()
{std::cout << std::boolalpha;std::cout << "std::numeric_limits<bool>::min_exponent:                 "<< std::numeric_limits<bool>::min_exponent << std::endl;std::cout << "std::numeric_limits<char>::min_exponent:                 "<< std::numeric_limits<char>::min_exponent << std::endl;std::cout << "std::numeric_limits<signed char>::min_exponent:          "<< std::numeric_limits<signed char>::min_exponent << std::endl;std::cout << "std::numeric_limits<unsigned char>::min_exponent:        "<< std::numeric_limits<unsigned char>::min_exponent << std::endl;std::cout << "std::numeric_limits<wchar_t>::min_exponent:              "<< std::numeric_limits<wchar_t>::min_exponent << std::endl;std::cout << "std::numeric_limits<char16_t>::min_exponent:             "<< std::numeric_limits<char16_t>::min_exponent << std::endl;std::cout << "std::numeric_limits<char32_t>::min_exponent:             "<< std::numeric_limits<char32_t>::min_exponent << std::endl;std::cout << "std::numeric_limits<short>::min_exponent:                "<< std::numeric_limits<short>::min_exponent << std::endl;std::cout << "std::numeric_limits<unsigned short>::min_exponent:       "<< std::numeric_limits<unsigned short>::min_exponent << std::endl;std::cout << "std::numeric_limits<int>::min_exponent:                  "<< std::numeric_limits<int>::min_exponent << std::endl;std::cout << "std::numeric_limits<unsigned int>::min_exponent:         "<< std::numeric_limits<unsigned int>::min_exponent << std::endl;std::cout << "std::numeric_limits<long>::min_exponent:                 "<< std::numeric_limits<long>::min_exponent << std::endl;std::cout << "std::numeric_limits<unsigned long>::min_exponent:        "<< std::numeric_limits<unsigned long>::min_exponent << std::endl;std::cout << "std::numeric_limits<long long>::min_exponent:            "<< std::numeric_limits<long long>::min_exponent << std::endl;std::cout << "std::numeric_limits<unsigned long long>::min_exponent:   "<< std::numeric_limits<unsigned long long>::min_exponent << std::endl;std::cout << "std::numeric_limits<float>::min_exponent:                "<< std::numeric_limits<float>::min_exponent << std::endl;std::cout << "std::numeric_limits<double>::min_exponent:               "<< std::numeric_limits<double>::min_exponent << std::endl;std::cout << "std::numeric_limits<long double>::min_exponent:          "<< std::numeric_limits<long double>::min_exponent << std::endl;std::cout << "std::numeric_limits<std::string>::min_exponent:          "<< std::numeric_limits<std::string>::min_exponent << std::endl;std::cout << "std::numeric_limits<SName>::min_exponent:                "<< std::numeric_limits<SName>::min_exponent << std::endl;std::cout << "std::numeric_limits<SPartSpec>::min_exponent:            "<< std::numeric_limits<SPartSpec>::min_exponent << std::endl;return 0;
}

输出

这篇关于C++标准模板(STL)- 类型支持 (数值极限,max_digits10,radix,min_exponent)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/209986

相关文章

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2