命名空间
变体
操作

std::numeric_limits<T>::digits10

来自 cppreference.cn
< cpp‎ | 类型‎ | 数值限制
 
 
 
 
 
static const int digits10;
(C++11 前)
static constexpr int digits10;
(C++11 起)

std::numeric_limits<T>::digits10 的值是类型 T 可以不变地表示的十进制位数,也就是说,任何具有此数量的有效十进制数字的数字,都可以转换为类型 T 的值,然后再转换回十进制形式,而不会因舍入或溢出而改变。对于基数radix类型,它是digits()的值(对于浮点类型,是digits - 1),乘以log10(radix)并向下取整。

[编辑] 标准特化

T std::numeric_limits<T>::digits10 的值
/* 未特化 */ 0
bool 0
char std::numeric_limits<char>::digits * std::log10(2)
signed char std::numeric_limits<signed char>::digits * std::log10(2)
unsigned char std::numeric_limits<unsigned char>::digits * std::log10(2)
wchar_t std::numeric_limits<wchar_t>::digits * std::log10(2)
char8_t (C++20起) std::numeric_limits<char8_t>::digits * std::log10(2)
char16_t (C++11起) std::numeric_limits<char16_t>::digits * std::log10(2)
char32_t (C++11起) std::numeric_limits<char32_t>::digits * std::log10(2)
short std::numeric_limits<short>::digits * std::log10(2)
unsigned short std::numeric_limits<unsigned short>::digits * std::log10(2)
int std::numeric_limits<int>::digits * std::log10(2)
unsigned int std::numeric_limits<unsigned int>::digits * std::log10(2)
long std::numeric_limits<long>::digits * std::log10(2)
unsigned long std::numeric_limits<unsigned long>::digits * std::log10(2)
long long (C++11起) std::numeric_limits<long long>::digits * std::log10(2)
unsigned long long (C++11起) std::numeric_limits<unsigned long long>::digits * std::log10(2)
float FLT_DIG(对于IEEE float,值为6
double DBL_DIG(对于IEEE double,值为15
long double LDBL_DIG(对于80位Intel long double,值为18;对于IEEE四精度,值为33

[编辑] 示例

一个8位二进制类型可以精确地表示任何两位十进制数,但无法表示三位十进制数256..999。8位类型的 digits10 值为 2 (8 * std::log10(2) 为 2.41)。

标准32位IEEE 754浮点类型有一个24位小数部分(写入23位,一位隐含),这可能表明它可以表示7位小数(24 * std::log10(2) 为 7.22),但相对舍入误差是非均匀的,有些具有7位小数的浮点值在转换为32位浮点数并返回时无法保持不变:最小的正例是 8.589973e9,在往返转换后变为 8.589974e9。这些舍入误差不能超过表示中的一位,并且 digits10 计算为 (24 - 1) * std::log10(2),结果为 6.92。向下取整结果为 6。

同样,16位数字字符串 9007199254740993 在文本->双精度->文本往返转换后无法保持不变,变为 9007199254740992:64位IEEE 754双精度类型仅保证15位十进制数的往返转换。

[编辑] 另请参阅

[静态] (C++11)
区分此类型所有值所需的十进制位数
(public static member constant) [编辑]
[静态]
给定类型的表示所用的基或整数底
(public static member constant) [编辑]
[静态]
能无变化表示的 radix 数字位数
(public static member constant) [编辑]
比最小负数基幂大一,该基幂是有效的正规化浮点值
(public static member constant) [编辑]
比最大整数基幂大一,该基幂是有效的有限浮点值
(public static member constant) [编辑]