浮点数的整数部分中的 10 进制数字的最大位数是多少
What Are the Maximum Number of Base-10 Digits in the Integral Part of a Floating Point Number
我想知道标准中是否有一些东西,比如 #define
或 numeric_limits
中的东西会告诉我整数部分中以 10 为底的数字的最大数目浮点型。
例如,如果我有一些浮点类型,其最大值是:1234.567。我想要标准中定义的东西,它会告诉我该类型为 4。
我可以选择这样做吗?
template <typename T>
constexpr auto integral_digits10 = static_cast<int>(log10(numeric_limits<T>::max())) + 1;
正如 Nathan Oliver 在评论中指出的那样,C++ 提供了 std::numeric_limits<T>::digits10
。
the number of base-10 digits that can be represented by the type T
without change, that is, any number with this many decimal digits can be converted to a value of type T
and back to decimal form, without change due to rounding or overflow. For base-radix
types, it is the value of digits
(digits
-1 for floating-point types) multiplied by log10(radix
) and rounded down.
对此的解释是explained by Rick Regan here。总之,如果您的二进制浮点格式可以在有效数中存储 b 位,那么您保证能够往返高达 d 十进制数字,其中 d 是满足
的最大整数
10d < 2b-1
对于IEEE754 binary64(如今大多数系统上C++中的标准double
),则b = 53,而2b-1 = 4,503,599,627,370,496,所以格式只保证能表示d = 15位数字。
但是这个结果适用于所有位,而你只问整数部分。然而,我们可以很容易地找到一个反例,选择 x = 2b+1,这是最小的不能用格式表示的整数:对于 binary64,这是 9,007,199,254,740,993,它也恰好有 16 位数字,因此 will need to be rounded。
您要查找的值是 max_exponent10
其中:
Is the largest positive number n such that 10n is a representable finite value of the floating-point type
因为这段关系:
log10x = n
10n = x
你的计算正在做,正在寻找 n 第一个等式的工作方式:
log10(numeric_limits<T>::max())
max_exponent10
的定义解释说它使用 10n + 1 会大于 numeric_limits<T>::max()
但 10n 小于或等于 numeric_limits<T>::max()
。 所以 numeric_limits<T>::max_exponent10
就是您要找的。
请注意,在您的示例中,您仍然需要 + 1
来说明 1 的位置。 (因为 log101 = 0)所以代表 numeric_limits<T>::max()
所需的 10 位数字的数量将是:
numeric_limits<T>::max_exponent10 + 1
如果您想手动验证,可以在此处查看:
我想知道标准中是否有一些东西,比如 #define
或 numeric_limits
中的东西会告诉我整数部分中以 10 为底的数字的最大数目浮点型。
例如,如果我有一些浮点类型,其最大值是:1234.567。我想要标准中定义的东西,它会告诉我该类型为 4。
我可以选择这样做吗?
template <typename T>
constexpr auto integral_digits10 = static_cast<int>(log10(numeric_limits<T>::max())) + 1;
正如 Nathan Oliver 在评论中指出的那样,C++ 提供了 std::numeric_limits<T>::digits10
。
the number of base-10 digits that can be represented by the type
T
without change, that is, any number with this many decimal digits can be converted to a value of typeT
and back to decimal form, without change due to rounding or overflow. For base-radix
types, it is the value ofdigits
(digits
-1 for floating-point types) multiplied by log10(radix
) and rounded down.
对此的解释是explained by Rick Regan here。总之,如果您的二进制浮点格式可以在有效数中存储 b 位,那么您保证能够往返高达 d 十进制数字,其中 d 是满足
的最大整数10d < 2b-1
对于IEEE754 binary64(如今大多数系统上C++中的标准double
),则b = 53,而2b-1 = 4,503,599,627,370,496,所以格式只保证能表示d = 15位数字。
但是这个结果适用于所有位,而你只问整数部分。然而,我们可以很容易地找到一个反例,选择 x = 2b+1,这是最小的不能用格式表示的整数:对于 binary64,这是 9,007,199,254,740,993,它也恰好有 16 位数字,因此 will need to be rounded。
您要查找的值是 max_exponent10
其中:
Is the largest positive number n such that 10n is a representable finite value of the floating-point type
因为这段关系:
log10x = n
10n = x
你的计算正在做,正在寻找 n 第一个等式的工作方式:
log10(numeric_limits<T>::max())
max_exponent10
的定义解释说它使用 10n + 1 会大于 numeric_limits<T>::max()
但 10n 小于或等于 numeric_limits<T>::max()
。 所以 numeric_limits<T>::max_exponent10
就是您要找的。
请注意,在您的示例中,您仍然需要 + 1
来说明 1 的位置。 (因为 log101 = 0)所以代表 numeric_limits<T>::max()
所需的 10 位数字的数量将是:
numeric_limits<T>::max_exponent10 + 1
如果您想手动验证,可以在此处查看: