These are the human proofs of Fast inverse square root, at full length.
The fast inverse square root has relative error at most 0.0017525 full
This proof shows: correctness.
Setting.x is a binary32 float with 2−125≤x≤(2−2−23)2127. All floating-point operations are binary32 operations, correctly rounded to nearest, so each one satisfies the standard modelfl(aopb)=(aopb)(1+δ), ∣δ∣≤u, with u=2−24, when its exact result is in the normal range. R=0x5F3759DF. We write y0 for the value of line L4, y1 for the result, ε0=y0x−1 and ε1=y1x−1.
Theorem.−0.0017525≤ε1≤2×10−7.
Step 0 (the encoding). A positive normal binary32 float 2E(1+k/223), with −126≤E≤127 and 0≤k<223, has the pattern 223(E+127)+k. Conversely each pattern in [0x00800000,0x7F7FFFFF] is a positive normal float. Line L1 is exact: x/2≥2−126 is normal, and halving a normal float only lowers its exponent.
Step 1 (scaling). Let i=BITS(x). The domain gives 0x01000000≤i≤0x7F7FFFFF, so 0x00800000≤⌊i/2⌋≤0x3FBFFFFF and the pattern g=R−⌊i/2⌋ of line L3 lies in [0x1F7759E0,0x5EB759DF], so y0 is a positive normal float. If x and 4x are both in the domain, then BITS(4x)=i+224 by Step 0, so ⌊BITS(4x)/2⌋=⌊i/2⌋+223 and the pattern of the guess for 4x is g−223. Both patterns are normal, so by Step 0 the guess for 4x is y0/2. Hence ε0(4x)=(y0/2)4x−1=ε0(x). Each x in the domain is 4jx′ for an integer j and an x′∈[1,4), with all the intermediate values 4lx′ in the domain (the domain is a union of whole binades from 2−125 on). So the set of values of ε0 on the domain is the set of its values on the floats of [1,4).
Step 2 (the guess on [1, 4)). Let tA=7821791/223 and tB=3627487/223; these are the fraction fields of 0x3F7759DF and 0x3F3759DF, both of which have exponent field 126. Let η=(k/2−⌊k/2⌋)/223∈{0,2−24}.
Piece A:x=1+m, m=k/223, 0≤k<223. Then i=0x3F800000+k and g=0x3F7759DF−⌊k/2⌋. Since ⌊k/2⌋<222<7821791, the exponent field stays 126 and y0=21(1+tA−m/2+η).
Piece B1:x=2(1+m), m=k/223, 0≤k≤7254975. Then g=0x3F3759DF−⌊k/2⌋ with ⌊k/2⌋≤3627487, so y0=21(1+tB−m/2+η).
Piece B2:x=2(1+m), 7254976≤k<223. Now ⌊k/2⌋>3627487, the subtraction borrows from the exponent field, which becomes 125, and the fraction field is 223+3627487−⌊k/2⌋. So y0=41(2+tB−m/2+η).
Drop η and define, for real m in the closed range of each piece,
Then ε0=g(m)+cηx with c=21 on pieces A and B1 and c=41 on B2. Since 0≤η≤2−24 and x<2, we get g(m)≤ε0≤g(m)+6×10−8.
Each g has the form L(m)m+1 times a constant, minus 1, with L linear, decreasing and positive on the range. Then (L1+m)′′=2L′(1+m)′+L(1+m)′′<0, so each g is concave: its minimum over a closed interval is at an endpoint and its maximum is at the zero of g′ when that zero is inside. The derivative of (a−m/2)1+m is (a−1−3m/2)/(21+m), which vanishes at m=32(a−1). This gives the critical points 32tA≈0.6216, 32tB≈0.2883 and 32(1+tB)≈0.9550, each inside its piece. The values, rounded outward in the ninth decimal, are:
piece
range of m
at the left end
at the right end
at the critical point
A
[0,1]
−0.033784926
0.012881071
0.032508610
B1
[0,7254975/223]
0.012881071
−0.034375800
0.033960196
B2
[7254976/223,1]
−0.034375769
−0.033784926
−0.033593969
So g≥−0.0343758 everywhere and g≤0.0339602 everywhere, and with the η term,
−0.0343758≤ε0≤0.0339603.
By Step 1 this holds on the whole domain.
Step 3 (an exact Newton step). Let a=1+ε0=y0x and z=(x/2)y02=a2/2. Then a(1.5−z)=a(3−a2)/2, and with a=1+ε0,
Write εˉ1=−21ε02(3+ε0). For ε0∈[−0.0343758,0.0339603], εˉ1≤0, and ∣εˉ1∣=21ε02(3+ε0) is increasing in ∣ε0∣ on each side of 0; its values at the two ends are 0.00175224 and 0.00174954. So −0.0017523≤εˉ1≤0.
Step 4 (the roundings of line L5). All exact intermediate results are normal: x/2 is, (x/2)y0=ax/2 lies in [2−64,264], and the other values are near 21 or 1, or near y0. So the standard model gives
Here 0<a≤1.0339603 and 0<z=a2/2≤0.534537, so ∣azθ∣≤0.55270⋅γ2≤1.1055u. With A=1+εˉ1−azθ, we have 1−0.0017523−1.1055u≤A≤1+1.1055u, and (1−u)2≤(1+δ3)(1+δ4)≤(1+u)2. Hence
Since u=2−24<5.97×10−8, 3.106u<1.86×10−7. So −0.0017525≤ε1≤2×10−7. ■
Remark. The bound is tight up to the rounding term: the float x=2(1+7254974/223)=3.7297205924987793 has y0=0.5 exactly, ε0=−0.0343758 to 7 digits, and ε1=−0.0017523.