Algorithm Search⌘K

Proofs: Payne-Hanek argument reduction

These are the human proofs of Payne-Hanek argument reduction, at full length.

Payne-Hanek reduction returns the exact quadrant and an accurate reduced argument full

This proof shows: correctness.

Setting. xx is a finite binary64 number with ∣x∣>fl(π/4)|x| > \mathrm{fl}(\pi/4). Write ∣x∣=M2E|x| = M 2^{E} with MM an integer, 0<M<2530 < M < 2^{53}, and E≥−53E \ge -53. Write 2/π=∑i≥1bi2−i2/\pi = \sum_{i \ge 1} b_i 2^{-i} with bi∈{0,1}b_i \in \{0, 1\}. The table holds b1,…,bNb_1, \ldots, b_N with N≥i1N \ge i_1 for every exponent that occurs. P≥55P \ge 55 and Q≥1Q \ge 1 are integers, and CC is an integer with ∣C−2Qπ/2∣≤1/2|C - 2^{Q}\pi/2| \le 1/2. All of lines L2 to L8 except the last conversion in L8 are exact integer operations.

Theorem. Lines L1 to L10 return k∈{0,1,2,3}k \in \{0, 1, 2, 3\} and floats rhi,rlor_{hi}, r_{lo} such that, for some integer jj,

x=(4j+k)π2+r,∣r−FC2P+Q σ∣≤253−Pπ2+2−Q−2,x = (4j + k)\frac{\pi}{2} + r, \qquad \left| r - \frac{F C}{2^{P+Q}}\,\sigma \right| \le 2^{53 - P}\frac{\pi}{2} + 2^{-Q-2},

where σ=sign⁡(x)\sigma = \operatorname{sign}(x), FF is the value after line L7, and rhi=fl(σFC/2P+Q)r_{hi} = \mathrm{fl}(\sigma F C / 2^{P+Q}), rlo=fl(σFC/2P+Q−rhi)r_{lo} = \mathrm{fl}(\sigma F C / 2^{P+Q} - r_{hi}). Also ∣FC/2P+Q∣≤π/4+2−Q−2|F C / 2^{P+Q}| \le \pi/4 + 2^{-Q-2}.

Step 1 (the dropped head). Let H=∑1≤i<i0biM2E−iH = \sum_{1 \le i < i_0} b_i M 2^{E - i}. If i0=1i_0 = 1 the sum is empty. Otherwise i0=E−1i_0 = E - 1, and each term has E−i≥2E - i \ge 2, so it is an integer multiple of M⋅4M \cdot 4. In both cases H=4hH = 4h for an integer hh.

Step 2 (the window). By the definition in L4, W=∑i0≤i≤i1bi2i1−iW = \sum_{i_0 \le i \le i_1} b_i 2^{i_1 - i}. So

∑i0≤i≤i1biM2E−i=MW2E−i1=Z2P,\sum_{i_0 \le i \le i_1} b_i M 2^{E - i} = M W 2^{E - i_1} = \frac{Z}{2^{P}},

since i1−E=Pi_1 - E = P. This is exact, because Z=MWZ = MW is an exact integer product.

Step 3 (the dropped tail). Let T=∑i>i1biM2E−iT = \sum_{i > i_1} b_i M 2^{E - i}. Then 0≤T≤M2E−i1∑t≥12−t=M2−P0 \le T \le M 2^{E - i_1} \sum_{t \ge 1} 2^{-t} = M 2^{-P}, and the inequality is strict because 2/π2/\pi is irrational, so not all bib_i with i>i1i > i_1 are 1. With M<253M < 2^{53} this gives 0≤T<253−P0 \le T < 2^{53 - P}.

Step 4 (the quadrant). Let Z=2PI+F0Z = 2^{P} I + F_0 with 0≤F0<2P0 \le F_0 < 2^{P}, so L6 sets k0=I mod 4k_0 = I \bmod 4 and F=F0F = F_0. Steps 1 to 3 give

∣x∣2π=4h+I+F02P+T.|x|\frac{2}{\pi} = 4h + I + \frac{F_0}{2^{P}} + T.

If F0<2P−1F_0 < 2^{P-1}, line L7 does nothing, and ∣x∣⋅2/π=4j′+k+F/2P+T|x| \cdot 2/\pi = 4j' + k + F/2^{P} + T with k=k0k = k_0 and 4j′=4h+I−k04j' = 4h + I - k_0. If F0≥2P−1F_0 \ge 2^{P-1}, line L7 sets k=(k0+1) mod 4k = (k_0 + 1) \bmod 4 and F=F0−2PF = F_0 - 2^{P}. The same identity holds with I+F0/2P=(I+1)+F/2PI + F_0/2^P = (I + 1) + F/2^{P}. In both cases F/2P∈[−1/2,1/2)F/2^{P} \in [-1/2, 1/2), and 4j′+k4j' + k is an integer congruent to kk modulo 4. Multiply by π/2\pi/2:

∣x∣=(4j′+k)π2+ρ,ρ=(F2P+T)π2.|x| = (4j' + k)\frac{\pi}{2} + \rho, \qquad \rho = \left(\frac{F}{2^{P}} + T\right)\frac{\pi}{2}.

This holds for whichever kk is chosen. So kk is never wrong: it is the quadrant that belongs to the returned reduced value.

Step 5 (the constant). ∣FC2P+Q−F2Pπ2∣=∣F∣2P⋅∣C−2Qπ/2∣2Q≤12⋅12Q+1=2−Q−2\left|\frac{F C}{2^{P+Q}} - \frac{F}{2^{P}}\frac{\pi}{2}\right| = \frac{|F|}{2^{P}} \cdot \frac{|C - 2^{Q}\pi/2|}{2^{Q}} \le \frac12 \cdot \frac{1}{2^{Q+1}} = 2^{-Q-2}. With Step 3, ∣ρ−FC2P+Q∣≤Tπ2+2−Q−2<253−Pπ2+2−Q−2\left|\rho - \frac{F C}{2^{P+Q}}\right| \le T\frac{\pi}{2} + 2^{-Q-2} < 2^{53-P}\frac{\pi}{2} + 2^{-Q-2}. Since ∣F∣/2P≤1/2|F|/2^{P} \le 1/2, also ∣FC/2P+Q∣≤π/4+2−Q−2|FC/2^{P+Q}| \le \pi/4 + 2^{-Q-2}.

Step 6 (the sign). If x>0x > 0, take j=j′j = j' and r=ρr = \rho. If x<0x < 0, then x=−∣x∣=(−4j′−k)π2−ρx = -|x| = (-4j' - k)\frac{\pi}{2} - \rho. Write −4j′−k=4j+((−k) mod 4)-4j' - k = 4j + ((-k) \bmod 4) for an integer jj. Line L9 returns (−k) mod 4(-k) \bmod 4 and negates the reduced value, so r=−ρr = -\rho, and the bound of Step 5 holds with σ=−1\sigma = -1. Negation is exact in floating point, so negating rhir_{hi} and rlor_{lo} equals rounding the negated value under round to nearest. This proves the theorem.

Step 7 (the early exit). If ∣x∣≤fl(π/4)|x| \le \mathrm{fl}(\pi/4), then ∣x∣<π/4|x| < \pi/4, and (k,r)=(0,x)(k, r) = (0, x) is exact with j=0j = 0.

Corollary (relative accuracy). The following fact is cited, not proved here: every binary64 xx with ∣x∣>π/4|x| > \pi/4 satisfies ∣r∣>2−62|r| > 2^{-62}. Ng (1992) and Muller (2016, Chapter 11) obtained this by a continued-fraction search over all exponents. The smallest value, at x=6381956970095103⋅2797x = 6381956970095103 \cdot 2^{797}, is about 2−60.92^{-60.9}. With P=Q=245P = Q = 245, the bound of the theorem is below 2−1902^{-190}. Write r~=σFC/2P+Q\tilde r = \sigma F C / 2^{P+Q}. Then ∣r~−r∣<2−128∣r∣|\tilde r - r| < 2^{-128}|r|. The conversion in L8 gives rhi=fl(r~)r_{hi} = \mathrm{fl}(\tilde r) and rlo=fl(r~−rhi)r_{lo} = \mathrm{fl}(\tilde r - r_{hi}), with no underflow because ∣r~∣>2−63|\tilde r| > 2^{-63}. So ∣rhi+rlo−r~∣≤u∣r~−rhi∣≤u2∣r~∣|r_{hi} + r_{lo} - \tilde r| \le u |\tilde r - r_{hi}| \le u^2 |\tilde r| with u=2−53u = 2^{-53}, and ∣rhi+rlo−r∣≤(2−106+2−127)∣r∣|r_{hi} + r_{lo} - r| \le (2^{-106} + 2^{-127})|r|. ■\blacksquare

Back to Payne-Hanek argument reduction