浮点数的积分部分中的最多10位数字是什么? [英] What Are the Maximum Number of Base-10 Digits in the Integral Part of a Floating Point Number
问题描述
我想知道标准中是否有一些东西,例如 #define
或 numeric_limits
会告诉我一个浮点类型的整数部分的十进制数字的最大数。
例如,如果我有一个浮点类型的最大值即:1234.567。
我可以选择这样做吗?
模板< typename T>
constexpr auto integral_digits10 = static_cast< int>(log10(numeric_limits T :: max()))+ 1;
您要查找的值是 max_exponent10
其中: p>
最大的正数n使得10 n 是浮点类型的可表示有限值
由于这种关系:
log 10 x = n 您的计算是这样做的,就是查找第一个方程式的运作方式: 请注意,在您的示例中,您仍然需要 如果您想手动验证,可以在这里查看: http://coliru.stacked-crooked.com/a/443e4d434cbcb2f6 I want to know if there is something in the standard, like a For example, if I have some floating point type the largest value of which is: 1234.567. I'd like something defined in the standard that would tell me 4 for that type. Is there an option to me doing this?
The value that you are looking for is Is the largest positive number n such that 10n is a representable finite value of the floating-point type Because of this relationship: log10x = n Your calculation is doing, is finding n the way the first equation works: The definition of Note that you will still need the If you feel like validating that by hand you can check here: http://coliru.stacked-crooked.com/a/443e4d434cbcb2f6 这篇关于浮点数的积分部分中的最多10位数字是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
10 n / p>
log10(numeric_limits< T> :: max())
> max_exponent10
的定义说明它使用10 n + 1 会大于 numeric_limits< T> :: max()
但是10 n 小于或等于 numeric_limits< T> :: max()
。 因此 numeric_limits< T> :: max_exponent10
是您要查找的内容。
+ 1
来计算1的位置。 (因为log 10 1 = 0)所以你需要表示 numeric_limits< T> :: max()
be:
numeric_limits< T> :: max_exponent10 + 1
#define
or something in numeric_limits
which would tell me the maximum number of base-10 digits in the integral part of a floating point type.template <typename T>
constexpr auto integral_digits10 = static_cast<int>(log10(numeric_limits<T>::max())) + 1;
max_exponent10
which:
10n = xlog10(numeric_limits<T>::max())
max_exponent10
is explaining that it is using a 10n + 1 would be larger than numeric_limits<T>::max()
but 10n is less than or equal to numeric_limits<T>::max()
. So numeric_limits<T>::max_exponent10
is what you're looking for.+ 1
as in your example, to account for the 1's place. (Because log101 = 0) So your the number of 10-based digits required to represent numeric_limits<T>::max()
will be:numeric_limits<T>::max_exponent10 + 1