Toby Bartels
the numerical analyst's quadratic formula

Consider the equation

ax 2+bx+c=0, a{x}^2 + b{x} + c = 0 ,

which we wish to solve for xx. Assume that aa, bb, and cc are real numbers and that we wish xx to be a complex number. Assume that the equation is nontrivial in that a0a \ne 0, c0c \ne 0, or b0b \ne 0 (in which case b>0b \gt 0 or b<0b \lt 0). Then we expect two solutions, although they might happen to be the same, or one or both of them might be infinite (and so not exist as a complex number). I'll call these two solutions x +x_+ and x x_-; which is which is somewhat arbitrary, but all of the formulas below are consistent about this.

If a0a \ne 0, then we may use the usual quadratic formula:

x +=b+b 24ac2a; x =bb 24ac2a. \begin {gathered} \displaystyle x_+ = \frac{-b + \sqrt{b^2 - 4a{c}}}{2a} ;\\ \displaystyle x_- = \frac{-b - \sqrt{b^2 - 4a{c}}}{2a} .\\ \end {gathered}

If c0c \ne 0, then we may rationalize the numerator:

x +=2cbb 24ac; x =2cb+b 24ac. \begin {gathered} \displaystyle x_+ = \frac{2c}{-b - \sqrt{b^2 - 4a{c}}} ;\\ \displaystyle x_- = \frac{2c}{-b + \sqrt{b^2 - 4a{c}}} .\\ \end {gathered}

If b>0b \gt 0, then we may mix the formulas:

x +=2cbb 24ac; x =bb 24ac2a. \begin {gathered} \displaystyle x_+ = \frac{2c}{-b - \sqrt{b^2 - 4a{c}}} ;\\ \displaystyle x_- = \frac{-b - \sqrt{b^2 - 4a{c}}}{2a} .\\ \end {gathered}

If b<0b \lt 0, then we may mix the formulas the other way:

x +=b+b 24ac2a; x =2cb+b 24ac. \begin {gathered} \displaystyle x_+ = \frac{-b + \sqrt{b^2 - 4a{c}}}{2a} ;\\ \displaystyle x_- = \frac{2c}{-b + \sqrt{b^2 - 4a{c}}} .\\ \end {gathered}

If more than one of the coefficients is nonzero, then you'll tend to minimize the risk of rounding errors (when using decimal approximations of the square roots) if you use the formula applicable to the coefficient that is the most nonzero (basically, the one with the largest absolute value, although the actual rule to pick the very best formula is more subtle).

Each of these formulas can sometimes give division by 00, but if you're using it when you're supposed to, then that can only happen when the corresponding solution is infinite, so that's perfectly appropriate. But if you use a formula when it's inapplicable, then you'll get 0/00/0 even if the solution is finite, and that's bad.

See quadratic formula for more advanced discussion.

Last revised on October 21, 2020 at 06:39:41. See the history of this page for a list of all contributions to it.