浮点数的积分部分中的最多10位数字是什么? [英] What Are the Maximum Number of Base-10 Digits in the Integral Part of a Floating Point Number

查看:177
本文介绍了浮点数的积分部分中的最多10位数字是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道标准中是否有一些东西,例如 #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

10 n

您的计算是这样做的,就是查找第一个方程式的运作方式:

  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 

如果您想手动验证,可以在这里查看:



http://coliru.stacked-crooked.com/a/443e4d434cbcb2f6


I want to know if there is something in the standard, like a #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.

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?

template <typename T>
constexpr auto integral_digits10 = static_cast<int>(log10(numeric_limits<T>::max())) + 1;

解决方案

The value that you are looking for is max_exponent10 which:

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
10n = x

Your calculation is doing, is finding n the way the first equation works:

log10(numeric_limits<T>::max())

The definition of 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.

Note that you will still need the + 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

If you feel like validating that by hand you can check here:

http://coliru.stacked-crooked.com/a/443e4d434cbcb2f6

这篇关于浮点数的积分部分中的最多10位数字是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆