浮点数输出 | 一种完全正确的实现方法(3)

注:本文为 "浮点数运算" 相关译文,机翻未校。

略作重排,如有内容异常,请看原文。

csdn 篇幅所限,分篇连载。


5. Errol2: Almost Optimal

5. Errol2:近乎最优

Recall (from Figure 1) that Grisu3 and Errol1 return sub-optimal conversions if there is a number in the uncovered intervals that is shorter than the shortest number in the narrow interval. As Errol1 uses a more accurate H P HP HP format than Grisu3 (106-bits vs 64-bits), Errol1 is able to expand the size of the narrow interval and shrink the size of the uncovered intervals, thereby lowering the suboptimality rate by an order of magnitude as smaller uncovered intervals are less likely to contain a shorter number. Surprisingly, we found that further expanding the narrow interval, e.g. by using even higher precision, does not improve optimality. Next, we describe how we empirically establish the above phenomenon (§ 5.1), analytically characterize the inputs where sub-optimal outputs are possible (§ 5.2), and how insights from the above yield Errol2, which is fast, correct and optimal on 99.9999999% of all inputs (§ 5.3).

回顾(图 1):如果未覆盖区间中存在比窄区间内最短数更短的数,Grisu3 与 Errol1 就会返回非最优转换。由于 Errol1 使用比 Grisu3 更精确的 H P HP HP 格式(106 位 vs 64 位),Errol1 能够扩大窄区间、缩小未覆盖区间,从而将非最优率降低一个数量级,因为更小的未覆盖区间更不可能包含更短的数。出乎意料的是,我们发现进一步扩大窄区间(例如使用更高精度)并不能提升最优性。下文介绍我们如何通过实验证实上述现象(§ 5.1),如何解析刻画可能产生非最优输出的输入(§ 5.2),以及如何基于上述结论设计 Errol2,使其快速、正确,并对 99.9999999% 的输入达到最优(§ 5.3)。

Figure 11: Optimality failures, with Errol1 in black and Grisu3 in grey. The x-axis represents binary exponents. For each exponent e e e on the x-axis, we randomly sample about 500,000 values in the range [ 2 e , 2 e + 1 ) [2^e, 2^{e+1}) [2e,2e+1), and we plot along the y-axis, the percentage of those 500,000 values for which the conversion is suboptimal. The left plot shows that Errol1 has zero failures for nearly the entire input space (with an average of 0.027% failure rate) whereas Grisu3 fails consistently over the entire range (with an average of 0.5% failure rate). The left plot only shows the points with less than 1% error. The omitted points all fall in the range of exponents shown in the right plot, where we have focused the x-axis on these points and expanded the y-axis to see all the points above 1% error.

图 11:最优性失败情况,黑色为 Errol1,灰色为 Grisu3。x 轴为二进制指数。对 x 轴上每个指数 e e e,我们在区间 [ 2 e , 2 e + 1 ) [2^e, 2^{e+1}) [2e,2e+1) 内随机采样约 500 000 个值,并在 y 轴绘制这 500 000 个值中转换非最优的百分比。左图显示 Errol1 在几乎整个输入空间上失败率为 0(平均失败率 0.027%),而 Grisu3 在整个范围内持续失败(平均失败率 0.5%)。左图仅显示误差小于 1% 的点,被省略的点全部落在右图所示的指数范围内,右图将 x 轴聚焦于这些点,并放大 y 轴以查看所有误差大于 1% 的点。

5.1 Empirically Locating Optimality Failures

5.1 实验定位最优性失败点

To empirically investigate the source of optimality failures, we randomly generated and converted one billion double-precision floating-point values and checked the results for optimality.

为实验研究最优性失败的来源,我们随机生成并转换 10 亿个双精度浮点数值,并检查结果的最优性。

The Bad News: Pathological Midpoints. From the data, we observed the pattern that every observed failure was caused by a pathological midpoint ( m ˉ − \bar{m}^- mˉ− or m ˉ + \bar{m}^+ mˉ+) whose length was shorter than every number inside the exclusive interval ( m ˉ − , m ˉ + ) (\bar{m}^-, \bar{m}^+) (mˉ−,mˉ+). Because the midpoints are the most extreme points of the rounding interval, any narrowing -- regardless of how precise -- can never generate such numbers and hence, will not be optimal.
坏消息:病态中点 :从数据中我们观察到一个规律:所有观测到的失败均由病态中点 ( m ˉ − \bar{m}^- mˉ− 或 m ˉ + \bar{m}^+ mˉ+)引起,该中点的长度比开区间 ( m ˉ − , m ˉ + ) (\bar{m}^-, \bar{m}^+) (mˉ−,mˉ+) 内的所有数都更短。由于中点是舍入区间的最极端位置,任何窄化------无论精度多高------都永远无法生成这样的数,因此无法达到最优。

The Good News: Pathology is Contained. Fortunately, we discovered that the distribution of optimality failures forms a very unexpected and useful pattern. We split the space of inputs into 2098 bins where each bin had approximately 500,000 values drawn from the interval [ 2 e , 2 e + 1 ) [2^e, 2^{e+1}) [2e,2e+1). For every bin, we computed the percentage of optimality failures and show the results in Figure 11. Curiously, we found that pathological midpoints spike around e = 58 e=58 e=58 and exponentially decay as e e e goes to 0. Outside the range 2 58 2^{58} 258 to 2 82 2^{82} 282, we observed no optimality failures.
好消息:病态性可被限定 :幸运的是,我们发现最优性失败的分布呈现出非常意外且有用的模式。我们将输入空间划分为 2098 个桶,每个桶包含约 500 000 个从区间 [ 2 e , 2 e + 1 ) [2^e, 2^{e+1}) [2e,2e+1) 中抽取的值。对每个桶,我们计算最优性失败的百分比,结果如图 11 所示。奇怪的是,我们发现病态中点在 e = 58 e=58 e=58 附近激增,并随 e e e 趋近于 0 指数衰减。在 2 58 2^{58} 258 到 2 82 2^{82} 282 范围之外,我们未观测到任何最优性失败。

5.2 Analytically Characterizing Optimality Failures

5.2 解析刻画最优性失败

Next, we present a theoretical analysis that explains the curious spike, i.e. both the cause and location of pathological midpoints. First, we show that pathological midpoints must be integers, i.e. they have no bits right of the radix point. Second, we demonstrate that they become more rare as the values get larger; and after a certain point, the midpoints become so large that pathological cases become extinct. Consequently, for double-precision numbers, pathological midpoints must be integers in [ 2 54 , 2 131 ] [2^{54}, 2^{131}] [254,2131].

接下来,我们给出理论分析,解释这一奇怪的峰值,即病态中点的成因与位置。首先,我们证明病态中点必须是整数,即小数点右侧没有位。其次,我们证明数值越大,病态中点越罕见;超过某一阈值后,中点变得极大,病态情况完全消失。因此,对于双精度数,病态中点必须是 [ 2 54 , 2 131 ] [2^{54}, 2^{131}] [254,2131] 内的整数。

Theorem 6. (Pathological Midpoints) If m ˉ \bar{m} mˉ is a pathological midpoint then: (i) m ˉ \bar{m} mˉ must be an integer, and (ii) 2 54 ≤ m ˉ ≤ 2 131 2^{54} \le \bar{m} \le 2^{131} 254≤mˉ≤2131
定理 6(病态中点) :若 m ˉ \bar{m} mˉ 是病态中点,则:(i) m ˉ \bar{m} mˉ 必为整数;(ii) 2 54 ≤ m ˉ ≤ 2 131 2^{54} \le \bar{m} \le 2^{131} 254≤mˉ≤2131

The proof of of the above requires a few basic definitions and facts about floating point numbers.

上述证明需要一些关于浮点数的基本定义与结论。

Index Decomposition. We say that d i d_i di is the digit at index i i i in r r r when r r r is expressed in position notation. For example, when r ≐ 3.1415 r \doteq 3.1415 r≐3.1415, we have d 0 ≐ 3 d_0 \doteq 3 d0≐3 and d − 4 ≐ 5 d_{-4} \doteq 5 d−4≐5, i.e. 3 and 5 are the digits at index 0 and −4 respectively. Thus, every real r ≐ ∑ k = − ∞ ∞ d k 10 k r \doteq \sum_{k=-\infty}^{\infty} d_k 10^k r≐∑k=−∞∞dk10k where d k d_k dk is the digit at index k k k in r r r.
位权分解 :当 r r r 按位权表示时,称 d i d_i di 为 r r r 在索引 i i i 处的数字。例如,当 r ≐ 3.1415 r \doteq 3.1415 r≐3.1415 时, d 0 ≐ 3 d_0 \doteq 3 d0≐3, d − 4 ≐ 5 d_{-4} \doteq 5 d−4≐5,即 3 和 5 分别是索引 0 和 −4 处的数字。因此,每个实数 r ≐ ∑ k = − ∞ ∞ d k 10 k r \doteq \sum_{k=-\infty}^{\infty} d_k 10^k r≐∑k=−∞∞dk10k,其中 d k d_k dk 是 r r r 在索引 k k k 处的数字。

Leftmost & Rightmost Index. For finite-length numbers, let:
最左与最右索引 :对有限长度数,令:
N ( r ) ≐ max ⁡ { k ∣ d k ≠ 0 } , M ( r ) = min ⁡ { k ∣ d k ≠ 0 } N(r) \doteq \max\left\{ k \mid d_k \neq 0\right\}, \quad M(r) = \min\left\{ k \mid d_k \neq 0\right\} N(r)≐max{k∣dk=0},M(r)=min{k∣dk=0}

We call N ( r ) N(r) N(r) (resp. M ( r ) M(r) M(r)) the left-most (resp. right-most) index of r r r. The left-most index is exactly computable as: N ( r ) ≐ ⌊ log ⁡ 10 r ⌋ N(r) \doteq \lfloor \log_{10} r \rfloor N(r)≐⌊log10r⌋. The right-most index is trickier in general but for integers is simply the number of trailing zeros, i.e. the maximum number of times the integer is evenly divisible by 10: M ( z ) ≐ f 10 ( z ) M(z) \doteq f_{10}(z) M(z)≐f10(z) where f 10 ( z ) f_{10}(z) f10(z) is the multiplicities of the factor 10 of z z z. For example, M ( 12300 ) = f 10 ( 12300 ) = 2 M(12300)=f_{10}(12300)=2 M(12300)=f10(12300)=2.

称 N ( r ) N(r) N(r)(对应 M ( r ) M(r) M(r))为 r r r 的最左索引 (对应最右索引 )。最左索引可精确计算: N ( r ) ≐ ⌊ log ⁡ 10 r ⌋ N(r) \doteq \lfloor \log_{10} r \rfloor N(r)≐⌊log10r⌋。最右索引一般更复杂,但对整数而言就是末尾 0 的个数,即该整数能被 10 整除的最大次数: M ( z ) ≐ f 10 ( z ) M(z) \doteq f_{10}(z) M(z)≐f10(z),其中 f 10 ( z ) f_{10}(z) f10(z) 是 z z z 中因子 10 的重数。例如, M ( 12300 ) = f 10 ( 12300 ) = 2 M(12300)=f_{10}(12300)=2 M(12300)=f10(12300)=2。

Length of a Number. The length of a real number r ∈ R r \in \mathbb{R} r∈R, written L ( r ) L(r) L(r), is defined as: L ( r ) ≐ N ( r ) − M ( r ) + 1 L(r) \doteq N(r)-M(r)+1 L(r)≐N(r)−M(r)+1. Thus, L ( r ) L(r) L(r) is the minimum number of digits necessary to write out the significand of r r r in decimal. Note that multiplication by (powers of) 10 does not effect a number's length; i.e. for any integer n n n, we have L ( r ) = L ( r × 10 n ) L(r)=L(r \times 10^n) L(r)=L(r×10n). For example, L ( 1.23 ) = L ( 1.23 × 10 − 6 ) = 3 L(1.23)=L(1.23 \times 10^{-6})=3 L(1.23)=L(1.23×10−6)=3.
数的长度 :实数 r ∈ R r \in \mathbb{R} r∈R 的长度记为 L ( r ) L(r) L(r),定义为: L ( r ) ≐ N ( r ) − M ( r ) + 1 L(r) \doteq N(r)-M(r)+1 L(r)≐N(r)−M(r)+1。因此, L ( r ) L(r) L(r) 是用十进制写出 r r r 的尾数所需的最少数字个数。注意乘以 10 的幂不改变数的长度;即对任意整数 n n n,有 L ( r ) = L ( r × 10 n ) L(r)=L(r \times 10^n) L(r)=L(r×10n)。例如, L ( 1.23 ) = L ( 1.23 × 10 − 6 ) = 3 L(1.23)=L(1.23 \times 10^{-6})=3 L(1.23)=L(1.23×10−6)=3。

Lemma 1. (Shifting) In a floating-point format with p p p bits of precision, if v ^ \hat{v} v^ is a floating point number, and m ˉ \bar{m} mˉ is a midpoint adjacent to v ^ \hat{v} v^ then there exists: (1) an integer e e e, (2) a natural number z v ^ < 2 p + 1 z_{\hat{v}}<2^{p+1} zv^<2p+1 and (3) an odd natural number 2 p + 1 ≤ z m ˉ ≤ 2 p + 2 2^{p+1} \le z_{\bar{m}} \le 2^{p+2} 2p+1≤zmˉ≤2p+2, such that: v ^ = z v ^ 2 e \hat{v}=z_{\hat{v}} 2^e v^=zv^2e and m ˉ = z m ˉ 2 e − 1 \bar{m}=z_{\bar{m}} 2^{e-1} mˉ=zmˉ2e−1
引理 1(移位) :在精度为 p p p 位的浮点数格式中,若 v ^ \hat{v} v^ 是浮点数, m ˉ \bar{m} mˉ 是与 v ^ \hat{v} v^ 相邻的中点,则存在:(1) 整数 e e e;(2) 自然数 z v ^ < 2 p + 1 z_{\hat{v}}<2^{p+1} zv^<2p+1;(3) 奇自然数 2 p + 1 ≤ z m ˉ ≤ 2 p + 2 2^{p+1} \le z_{\bar{m}} \le 2^{p+2} 2p+1≤zmˉ≤2p+2,使得: v ^ = z v ^ 2 e \hat{v}=z_{\hat{v}} 2^e v^=zv^2e 且 m ˉ = z m ˉ 2 e − 1 \bar{m}=z_{\bar{m}} 2^{e-1} mˉ=zmˉ2e−1。

To see that v ^ = z v ^ 2 e \hat{v}=z_{\hat{v}} 2^e v^=zv^2e, we need only shift the binary significand of v ^ \hat{v} v^ at most p p p digits to the right until it is a natural. The fact that z m ˉ z_{\bar{m}} zmˉ is odd follows from averaging two adjacent floating-point numbers v ^ \hat{v} v^ and v ^ + \hat{v}^+ v^+ after writing them in the above shifted form; the average of two adjacent integers is an irreducible rational of the form odd 2 \frac{\text{odd}}{2} 2odd and the factor two is incorporated into the 2 e 2^e 2e term.
v ^ = z v ^ 2 e \hat{v}=z_{\hat{v}} 2^e v^=zv^2e 成立的原因:只需将 v ^ \hat{v} v^ 的二进制尾数右移最多 p p p 位直至成为自然数。 z m ˉ z_{\bar{m}} zmˉ 为奇数的原因:将相邻浮点数 v ^ \hat{v} v^ 与 v ^ + \hat{v}^+ v^+ 写成上述移位形式后取平均;相邻整数的平均是形如 奇数 2 \frac{\text{奇数}}{2} 2奇数 的既约分数,因子 2 被并入 2 e 2^e 2e 项。

Proof of Theorem 6 (i): m ˉ \bar{m} mˉ must be an Integer. We prove this case by contradiction. Suppose that midpoint m ˉ \bar{m} mˉ is a non-integer rational i.e., has a fractional component ( e < 0 e<0 e<0). By Lemma 1, we have m ˉ = z m ˉ 2 − e ′ \bar{m}=z_{\bar{m}} 2^{-e'} mˉ=zmˉ2−e′ where e ′ = − e e'=-e e′=−e is nonnegative. Hence
定理 6 (i) 证明: m ˉ \bar{m} mˉ 必为整数 :反证法。假设中点 m ˉ \bar{m} mˉ 是非整数有理数,即含有小数部分( e < 0 e<0 e<0)。由引理 1, m ˉ = z m ˉ 2 − e ′ \bar{m}=z_{\bar{m}} 2^{-e'} mˉ=zmˉ2−e′,其中 e ′ = − e ≥ 0 e'=-e \ge 0 e′=−e≥0。因此
L ( m ˉ ) = L ( z m ˉ 2 − e ′ ) = L ( z m ˉ 10 − e ′ 5 − e ′ ) = L ( z m ˉ 5 e ′ ) L(\bar{m})=L\left(z_{\bar{m}} 2^{-e'}\right)=L\left(z_{\bar{m}} \frac{10^{-e'}}{5^{-e'}}\right)=L\left(z_{\bar{m}} 5^{e'}\right) L(mˉ)=L(zmˉ2−e′)=L(zmˉ5−e′10−e′)=L(zmˉ5e′)

where, by Lemma 1, z m ˉ z_{\bar{m}} zmˉ is an odd integer. Because 5 e ′ 5^{e'} 5e′ is also odd, z m ˉ 5 e ′ z_{\bar{m}} 5^{e'} zmˉ5e′ must be odd, and therefore not divisible by 10. This implies that the right-most index M ( m ˉ ) = 0 M(\bar{m})=0 M(mˉ)=0. Via the logarithmic form of N ( r ) N(r) N(r) we have

其中由引理 1, z m ˉ z_{\bar{m}} zmˉ 是奇整数。因为 5 e ′ 5^{e'} 5e′ 也是奇数,所以 z m ˉ 5 e ′ z_{\bar{m}} 5^{e'} zmˉ5e′ 必为奇数,因此不能被 10 整除。这意味着最右索引 M ( m ˉ ) = 0 M(\bar{m})=0 M(mˉ)=0。利用 N ( r ) N(r) N(r) 的对数形式可得
L ( m ˉ ) = N ( m ˉ ) − M ( m ˉ ) + 1 = ⌊ log ⁡ 10 z m ˉ 5 e ′ ⌋ + 1 \begin{aligned} L(\bar{m}) & =N(\bar{m})-M(\bar{m})+1 \\ & =\left\lfloor \log_{10} z_{\bar{m}} 5^{e'} \right\rfloor +1 \end{aligned} L(mˉ)=N(mˉ)−M(mˉ)+1=⌊log10zmˉ5e′⌋+1

By Lemma 1 the natural z m ˉ ≥ 2 p + 1 z_{\bar{m}} \ge 2^{p+1} zmˉ≥2p+1, so:

由引理 1,自然数 z m ˉ ≥ 2 p + 1 z_{\bar{m}} \ge 2^{p+1} zmˉ≥2p+1,因此:
L ( m ˉ ) ≥ ⌊ ( p + 1 ) log ⁡ 10 2 + e ′ log ⁡ 10 5 ⌋ + 1 L(\bar{m}) \ge \left\lfloor (p+1)\log_{10}2 + e'\log_{10}5 \right\rfloor +1 L(mˉ)≥⌊(p+1)log102+e′log105⌋+1

For double-precision floating-point numbers p = 53 p=53 p=53, so every noninteger midpoint has at least 17 digits. By the Maximum Length Theorem 5, Errol1 will produce an output of 17 digits or less, and so the non-integer midpoint m ˉ \bar{m} mˉ cannot be pathological.

对于双精度浮点数 p = 53 p=53 p=53,因此每个非整数中点至少有 17 位。由最大长度定理 5,Errol1 输出至多 17 位,因此非整数中点 m ˉ \bar{m} mˉ 不可能是病态的。

Proof of Theorem 6 (ii): 2 54 ≤ m ˉ ≤ 2 131 2^{54} \le \bar{m} \le 2^{131} 254≤mˉ≤2131. The lower bound 2 54 ≤ m ˉ 2^{54} \le \bar{m} 254≤mˉ follows as m ˉ \bar{m} mˉ must be an integer. Recall that the rightmost index M ( m ˉ ) M(\bar{m}) M(mˉ) is determined by multiplicities of factors ten, which further split into prime factors five and two. Hence, we have:
定理 6 (ii) 证明: 2 54 ≤ m ˉ ≤ 2 131 2^{54} \le \bar{m} \le 2^{131} 254≤mˉ≤2131 :下界 2 54 ≤ m ˉ 2^{54} \le \bar{m} 254≤mˉ 由 m ˉ \bar{m} mˉ 必为整数推出。回顾最右索引 M ( m ˉ ) M(\bar{m}) M(mˉ) 由因子 10 的重数决定,而 10 可分解为质因子 5 和 2。因此:
L ( m ˉ ) = ⌊ log ⁡ 10 m ˉ ⌋ − min ⁡ ( f 5 , f 2 ) + 1 L(\bar{m})=\lfloor \log_{10} \bar{m} \rfloor -\min(f_5,f_2)+1 L(mˉ)=⌊log10mˉ⌋−min(f5,f2)+1

where f 5 f_5 f5 and f 2 f_2 f2 are the multiplicities of prime factors five and two (of m ˉ \bar{m} mˉ). By the properties of min, the above implies:

其中 f 5 f_5 f5 与 f 2 f_2 f2 是 m ˉ \bar{m} mˉ 的质因子 5 和 2 的重数。由最小值性质,上式蕴含:
L ( m ˉ ) ≥ ⌊ log ⁡ 10 m ˉ ⌋ − f 5 + 1 L(\bar{m}) \ge \lfloor \log_{10} \bar{m} \rfloor -f_5+1 L(mˉ)≥⌊log10mˉ⌋−f5+1

From Lemma 1, only the z m ˉ z_{\bar{m}} zmˉ term can contain factors of five, and it can have at most f 5 ≤ ⌊ log ⁡ 5 2 p + 2 ⌋ f_5 \le \left\lfloor \log_5 2^{p+2} \right\rfloor f5≤⌊log52p+2⌋ factors, and so:

由引理 1,只有 z m ˉ z_{\bar{m}} zmˉ 项可能包含因子 5,且其重数至多为 f 5 ≤ ⌊ log ⁡ 5 2 p + 2 ⌋ f_5 \le \left\lfloor \log_5 2^{p+2} \right\rfloor f5≤⌊log52p+2⌋,因此:
L ( m ˉ ) ≥ ⌊ log ⁡ 10 m ˉ ⌋ − ⌊ log ⁡ 5 2 p + 2 ⌋ + 1 L(\bar{m}) \ge \left\lfloor \log_{10} \bar{m} \right\rfloor -\left\lfloor \log_5 2^{p+2} \right\rfloor +1 L(mˉ)≥⌊log10mˉ⌋−⌊log52p+2⌋+1

That is, the length L ( m ˉ ) L(\bar{m}) L(mˉ) grows with the size of the midpoint m ˉ \bar{m} mˉ. By the Maximum Length Theorem 5, Errol1 always prints decimals with 17 digits or less for double-precision numbers, so midpoints m ˉ \bar{m} mˉ cannot be pathological if:

即长度 L ( m ˉ ) L(\bar{m}) L(mˉ) 随中点 m ˉ \bar{m} mˉ 的大小增长。由最大长度定理 5,对双精度数 Errol1 总是输出至多 17 位十进制数,因此当中点 m ˉ \bar{m} mˉ 满足下式时不可能是病态的:
L ( m ˉ ) ≥ ⌊ log ⁡ 10 m ˉ ⌋ − ⌊ log ⁡ 5 2 54 ⌋ + 1 ≥ 17 L(\bar{m}) \ge \left\lfloor \log_{10} \bar{m} \right\rfloor -\left\lfloor \log_5 2^{54} \right\rfloor +1 \ge 17 L(mˉ)≥⌊log10mˉ⌋−⌊log5254⌋+1≥17

By solving the minimum m ˉ \bar{m} mˉ for which the above equation holds, we conclude that midpoints larger than 2 131 2^{131} 2131 cannot be pathological.

求解使上式成立的最小 m ˉ \bar{m} mˉ,可得大于 2 131 2^{131} 2131 的中点不可能是病态的。

5.3 Handling Pathological Midpoints

5.3 处理病态中点

The Pathological Midpoint Theorem 6 guarantees that midpoints are pathological only if they fall within a pathological range corresponding to midpoints that are small whole numbers between ( 2 54 , 2 131 ) (2^{54}, 2^{131}) (254,2131) (in our double-precision floating-point setting).

病态中点定理 6 保证:仅当中点落在病态范围内时才是病态的,在双精度浮点设置下,该范围对应 ( 2 54 , 2 131 ) (2^{54}, 2^{131}) (254,2131) 之间的小整数。

Errol2. Based on these properties, we designed the second iteration of our algorithm Errol2 which behaves as follows. If the input v ^ \hat{v} v^ is outside the pathological range, Errol2 converts v ^ \hat{v} v^ using the Errol1 algorithm. When the input v ^ \hat{v} v^ is in the pathological range [ 2 54 , 2 131 ) [2^{54}, 2^{131}) [254,2131), it is possible that one of the adjacent midpoints is pathological. Consequently, Errol2 computes the midpoints m ˉ − \bar{m}^- mˉ− and m ˉ + \bar{m}^+ mˉ+ exactly as an integer, using well known techniques for binary to decimal conversion for integers [9]. Errol2 then computes the output digits by invoking digits_hp (Figure 7) on the exact midpoints. As there is no narrowing, Theorem 3 ensures that the output digits are correct and optimal.
Errol2 :基于这些性质,我们设计了算法的第二版 Errol2,行为如下:若输入 v ^ \hat{v} v^ 在病态范围之外,Errol2 使用 Errol1 算法转换 v ^ \hat{v} v^。当输入 v ^ \hat{v} v^ 位于病态范围 [ 2 54 , 2 131 ) [2^{54}, 2^{131}) [254,2131) 内时,相邻中点可能是病态的。因此,Errol2 使用成熟的整数二进制转十进制技术 [9] 精确计算中点 m ˉ − \bar{m}^- mˉ− 与 m ˉ + \bar{m}^+ mˉ+。然后 Errol2 在精确中点上调用 digits_hp(图 7)计算输出数字。由于没有窄化,定理 3 保证输出数字正确且最优。

Empirical Evaluation. Note that Errol2 does not guarantee optimal conversion: narrowing outside the pathological range may still yield a sub-optimal output. However, we ran Errol2 on the sample set of one billion random inputs, and we observed zero optimality failures outside the pathological range (and of course, zero inside the range, thanks to the exact midpoints.) Therefore, we have empirically tested Errol2 to have an accuracy of approximately 99.9999999% or better.
实验评估:注意 Errol2 不保证最优转换:病态范围外的窄化仍可能产生非最优输出。但我们在 10 亿随机输入样本集上运行 Errol2,观察到病态范围外无最优性失败(当然,由于精确中点,范围内也无失败)。因此,实验验证 Errol2 的最优率约为 99.9999999% 或更高。