std::numeric_limits<T>::digits10
来自cppreference.com
< cpp | types | numeric limits
staticconstint digits10; | (C++11 前) | |
staticconstexprint digits10; | (C++11 起) | |
std::numeric_limits<T>::digits10 的值是类型 T
能无更改地表示的以 10 为底的位数,即任何拥有这么多十进制有效数字的数能转换成 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) |
signedchar | std::numeric_limits<signedchar>::digits*std::log10(2) |
unsignedchar | std::numeric_limits<unsignedchar>::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) |
unsignedshort | std::numeric_limits<unsignedshort>::digits*std::log10(2) |
int | std::numeric_limits<int>::digits*std::log10(2) |
unsignedint | std::numeric_limits<unsignedint>::digits*std::log10(2) |
long | std::numeric_limits<long>::digits*std::log10(2) |
unsignedlong | std::numeric_limits<unsignedlong>::digits*std::log10(2) |
longlong(C++11 起) | std::numeric_limits<longlong>::digits*std::log10(2) |
unsignedlonglong(C++11 起) | std::numeric_limits<unsignedlonglong>::digits*std::log10(2) |
float | FLT_DIG(对于 IEEE float 是 6) |
double | DBL_DIG(对于 IEEE double 是 15) |
longdouble | LDBL_DIG(对于 80 位英特尔 longdouble 是 15;对于 IEEE 四倍精度是 33) |
[编辑]示例
8 位二进制类型能准确表示任何二位十进制数,但不能表示 3 位十进制数 256..999。digits10
对 8 位类型的值是 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 在文本->double->文本回环中无法保持,它变成 9007199254740992:64 位 IEEE 754 double 类型只保证 15 位的舍入回环。
[编辑]参阅
[静态](C++11) | 区别所有此类型值所需的十进制位数 (公开静态成员常量) |
[静态] | 给定类型的表示所用的基或整数底 (公开静态成员常量) |
[静态] | 能无更改地表示的 radix 位数 (公开静态成员常量) |
[静态] | 底的该数次幂是合法正规浮点数的最小负数加一 (公开静态成员常量) |
[静态] | 底的该数次幂是合法有限浮点数的最大整数加一 (公开静态成员常量) |