[Math] Solving a quadratic equation with precision when using floating point variables

analysisfloating pointquadratics

I know how to solve a basic quadratic equation with the formula

$$t_{1,2}=\dfrac{-b\pm\sqrt{b^2-4ac}}{2a}$$

but I learned that if $b \approx \sqrt{b^2-4ac}$ floating point precision may give slightly wrong results and this approach is better. It works, indeed. But why? Is there a simple explanation on why this works?

Best Answer

We are accustomed to solving the equation $ax^2+bx+c=0$ by using the Quadratic Formula $$x=\frac{-b\pm \sqrt{b^2-4ac}}{2a}.$$ Alternately, we can use the Citardauq Formula $$x=\frac{2c}{-b\mp \sqrt{b^2-4ac}}.$$ When $|4ac|$ is small in comparison with $|b|$, one of the roots as computed by the Quadratic Formula may suffer serious loss of precision, because in the numerator we are finding the difference of large nearly equal quantities. Precisely that root is then nicely computed with no loss of precision by the Citardauq Formula.  

A simpler way of dealing with the problem is to note that the product of the roots of $ax^2+bx+c=0$ is $c/a$. So if we can compute one of the roots $r_1$ to high precision, the other root $r_2$ can be computed from $r_1r_2=c/a$ with no loss of precision.