可能影响双精度的最大小数位数 [英] Maximum number of decimal digits that can affect a double

查看:152
本文介绍了可能影响双精度的最大小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑十进制表示形式d1.d2d3d4d5 ... dnExxx其中xxx是一个任意的指数,而d1和dn都是非零的。

最大的n是已知的使得存在十进制表示d1.d2d3d4d5 ... dnExxx,使得间隔(d1.d2d3d4d5 ... dnExxx,d1.d2d3d4d5 ...((dn)+1)Exxx)包含IEEE 754 double? p>

至少应该是17个。问题是17以上有多少。

这个数字n有事可做在小数到双数的转换中足以考虑的位数,如 strtod()。我查看了 David M. Gay的实施的源代码,希望在那里找到答案。有一个典型的40,但不清楚这是一个良好的数学结果或只是一个统计安全的界限的结果。此外,关于截断的注释使得它听起来像0.500000000000000000000000000000000000000000000001可能会在上舍入模式下转换为0.5。

Musl的实现似乎读取了大约125 * 9位,这是很多。然后切换到sticky模式:



pre $ if(c!='0')x [KMAX-4] | = 1;

最后,如果将包含IEEE 754 double替换为contains the midpoint两个连续的IEEE 754双打?

解决方案

当你有一个奇数有效数的次正规数,也就是奇数倍 2 ^( - 1074),您有一个数字,其十进制表示中的最后一个非零数字是小数点后面的1074 th 。直接在小数点后面有大约300个零,所以这个数字有大约750-770个十进制数字。最小的正值低于正常值 2 ^( - 1074)有751位有效数字,最大正值低于正常值(2 ^ 52-1)* 2 ^( - 1074)有767个有效数字(我认为这是最大的)。

所以至少有一个序列 d1,...,d766 的十进制数字,使得在开放时间间隔内有一个IEEE754 double

 (d1.d2 ... d766E-308,d1.d2 ...(d766 + 1)E-308)

如果我们考虑包含两个连续的IEEE754 double s,因为低于正常的 double s的所有重要的十进制数字都是大致相同的数量,并且也是两个连续的中点。


$ b

在最坏的情况下,必须消耗整个数字序列(考虑0.5000000 ... 0001 1确定结果应该是 0.5 + 0.5 ^ 53 而不是 0.5 时,从零或上舍入)。

然而,只有

  floor(DBL_MANT_DIG * log 2 / log 10)+ 2 = 17 

需要显着的十进制数来区分不同的 double 值,所以一个相对容易的解析方法可能不是非常有效,至少17位数字(和指数)到最接近的 double ,并将输入字符串与 double value(及其邻居)。

Consider decimal representations of the form d1.d2d3d4d5...dnExxx where xxx is an arbitrary exponent and both d1 and dn are nonzero.

Is the maximum n known such that there exists a decimal representation d1.d2d3d4d5...dnExxx such that the interval (d1.d2d3d4d5...dnExxx, d1.d2d3d4d5...((dn)+1)Exxx) contains an IEEE 754 double?

n should be at least 17. The question is how much above 17.

This number n has something to do with the number of digits that it is enough to consider in a decimal-to-double conversion such as strtod(). I looked at the source code for David M. Gay's implementation hoping to find an answer there. There is an allusion to "40" but it is unclear whether this is a consequence of a sound mathematical result or just a statistically safe bound. Also the comment about "truncating" makes it sound like 0.5000000000000000000000000000000000000000000000000001 may be converted to 0.5 in round-upwards mode.

Musl's implementation seems to read approximately 125*9 digits, which is a lot. Then it switches to "sticky" mode:

if (c!='0') x[KMAX-4] |= 1;

Finally, how does the answer change when substituting "contains an IEEE 754 double" with "contains the midpoint of two consecutive IEEE 754 doubles"?

解决方案

When you have a subnormal number with odd significand, that is, an odd multiple of 2^(-1074), you have a number whose last nonzero digit in the decimal representation is the 1074th after the decimal point. You then have around 300 zeros directly following the decimal point, so the number has around 750-770 significant decimal digits. The smallest positive subnormal, 2^(-1074) has 751 significant digits, and the largest positive subnormal, (2^52-1)*2^(-1074) has 767 significant digits (I think that is the maximum).

So there is at least one sequence d1, ..., d766 of decimal digits such that there is an IEEE754 double in the open interval

(d1.d2...d766E-308, d1.d2...(d766 + 1)E-308)

The answer does not change much if we consider "contains the midpoint of two consecutive IEEE754 doubles", since subnormal doubles have all roughly the same amount of significant decimal digits, and the midpoint of two consecutive such too.

In the worst case, the entire digit sequence must be consumed (consider "0.5000000...0001" with arbitrarily many zeros before the final 1 that determines that the result shall be 0.5 + 0.5^53 and not 0.5 when rounding away from zero or up).

However, there are only

floor(DBL_MANT_DIG * log 2 / log 10) + 2 = 17

significant decimal digits necessary to distinguish between different double values, so a relatively easy, albeit probably not very efficient, method of parsing would be to parse the first (at least 17) digits (and the exponent) to the closest double, and compare the input string with the exact representation of that double value (and its neighbour).

这篇关于可能影响双精度的最大小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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