Algorithm Search⌘K

Proofs: Quadratic formula

These are the human proofs of Quadratic formula, at full length.

The stable quadratic formula returns the roots, each with relative error at most 7u/2 + O(u^2) full

This proof shows: correctness, error-bound.

Write h=b/2h = b/2, D=h2−acD = h^2 - ac and p(x)=ax2+bx+cp(x) = a x^2 + b x + c with a≠0a \ne 0. In floating point, fl⁡(x∘y)=(x∘y)(1+δ)\operatorname{fl}(x \circ y) = (x \circ y)(1 + \delta) and fl⁡(x)=x (1+δ)\operatorname{fl}(\sqrt x) = \sqrt x\,(1 + \delta) with ∣δ∣≤u|\delta| \le u when no underflow or overflow occurs (the standard model; see floating-point arithmetic).

Claim 1 (exact arithmetic). QUADRATIC returns the two roots of pp, with multiplicity.

Proof. Line L2 multiplies a,b,ca, b, c by the same nonzero number, which does not change the roots. Completing the square gives p(x)=a((x+h/a)2−D/a2)p(x) = a\big((x + h/a)^2 - D/a^2\big), so the roots are x=(−h±D)/ax = (-h \pm \sqrt D)/a, real and distinct if D>0D > 0, a double root −h/a-h/a if D=0D = 0, and the complex pair (−h±i−D)/a(-h \pm i\sqrt{-D})/a if D<0D < 0, which lines L5 and L6 return. Let D≥0D \ge 0 and σ=1\sigma = 1 if h≥0h \ge 0, σ=−1\sigma = -1 otherwise. Line L7 sets q=−(h+σD)q = -(h + \sigma\sqrt D), so x1=q/ax_1 = q/a is one of the two roots. The product of the roots is c/ac/a (expand a(x−x1)(x−x2)a(x - x_1)(x - x_2)). If q≠0q \ne 0, the other root is (c/a)/(q/a)=c/q(c/a)/(q/a) = c/q, which line L9 returns. If q=0q = 0, then h+σD=0h + \sigma\sqrt D = 0; both terms have the sign σ\sigma or are zero, so h=0h = 0 and D=0D = 0, hence ac=0ac = 0, so c=0c = 0 and p(x)=ax2p(x) = a x^2 has the double root 00 of line L8. □\square

Claim 2 (floating point). Assume binary64 with round to nearest and that, after line L2, no operation underflows or overflows. Let x1,x2x_1, x_2 be the exact roots of the stored scaled coefficients. Then the computed DD has the sign of DD, the computed real roots are xi(1+θi)x_i(1 + \theta_i), and the computed real and imaginary parts in the complex case are the exact ones times 1+θ1 + \theta, with every ∣θ∣≤72u+O(u2)|\theta| \le \tfrac72 u + O(u^2).

Proof. Line L2 multiplies by a power of two, which is exact. In line L3, h=b/2h = b/2 is exact, and D^=D(1+δ1)\hat D = D(1 + \delta_1) because DD is formed exactly and rounded once; in particular D^\hat D and DD have the same sign, and D^=0\hat D = 0 only if D=0D = 0.

Real case. s^=fl⁡(D^)=D (1+δ1)1/2(1+δ2)=D (1+ϵs)\hat s = \operatorname{fl}(\sqrt{\hat D}) = \sqrt D\,(1 + \delta_1)^{1/2}(1 + \delta_2) = \sqrt D\,(1 + \epsilon_s) with ∣ϵs∣≤32u+O(u2)|\epsilon_s| \le \tfrac32 u + O(u^2). Say h≥0h \ge 0 (the other case is the same with signs changed). Then q^=−fl⁡(h+s^)=−(h+D+D ϵs)(1+δ3)\hat q = -\operatorname{fl}(h + \hat s) = -(h + \sqrt D + \sqrt D\,\epsilon_s)(1 + \delta_3). Since h≥0h \ge 0 and D≥0\sqrt D \ge 0, ∣D ϵs∣≤∣h+D∣ ∣ϵs∣|\sqrt D\,\epsilon_s| \le |h + \sqrt D|\,|\epsilon_s|, so q^=q(1+ϵq)\hat q = q(1 + \epsilon_q) with ∣ϵq∣≤52u+O(u2)|\epsilon_q| \le \tfrac52 u + O(u^2). Then fl⁡(q^/a)=(q/a)(1+ϵq)(1+δ4)\operatorname{fl}(\hat q/a) = (q/a)(1 + \epsilon_q)(1 + \delta_4) and fl⁡(c/q^)=(c/q)(1+δ5)/(1+ϵq)\operatorname{fl}(c/\hat q) = (c/q)(1 + \delta_5)/(1 + \epsilon_q), and both factors are 1+θ1 + \theta with ∣θ∣≤72u+O(u2)|\theta| \le \tfrac72 u + O(u^2). If q^=0\hat q = 0 then q=0q = 0, and line L8 is exact.

Complex case. fl⁡(−h/a)=(−h/a)(1+δ6)\operatorname{fl}(-h/a) = (-h/a)(1 + \delta_6), and fl⁡(−D^/a)=(−D/a)(1+δ1)1/2(1+δ7)(1+δ8)\operatorname{fl}(\sqrt{-\hat D}/a) = (\sqrt{-D}/a)(1 + \delta_1)^{1/2}(1 + \delta_7)(1 + \delta_8), whose factor is 1+θ1 + \theta with ∣θ∣≤52u+O(u2)|\theta| \le \tfrac52 u + O(u^2). □\square

Remark (no cancellation is essential). The step ∣D ϵs∣≤∣h+D∣ ∣ϵs∣|\sqrt D\,\epsilon_s| \le |h + \sqrt D|\,|\epsilon_s| uses that hh and σD\sigma\sqrt D have the same sign. With the other sign, ∣h−D∣|h - \sqrt D| can be much smaller than D\sqrt D, and the same computation only gives an absolute error of about uDu\sqrt D; this is the failure of the textbook formula. ■\blacksquare

Back to Quadratic formula