TExcelTAD
Home › Algebra › The Quadratic Formula

The Quadratic Formula

The quadratic formula solves every quadratic equation, whether it factors or not. This lesson derives it from scratch by completing the square, uses it on a few equations and on a projectile problem, and then shows why the textbook version of the formula can give badly wrong answers in floating-point code, and what libraries do instead.

The formula

For ax2+bx+c=0ax^2 + bx + c = 0 with a≠0a \ne 0:

x=−b±b2−4ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

The ±\pm packs two answers into one line: one with ++, one with −-. The expression under the root, b2−4acb^2 - 4ac, is the discriminant; its sign decides whether there are two, one or no real roots (The Discriminant and Complex Roots).

Where it comes from

The derivation is completing the square done once with letters instead of numbers:

ax2+bx+c=0x2+bax+ca=0divide by a (allowed: a≠0)x2+bax=−camove the constantx2+bax+(b2a)2=(b2a)2−caadd (half the x-coefficient)2 to both sides(x+b2a)2=b24a2−4ac4a2left side is a perfect square(x+b2a)2=b2−4ac4a2x+b2a=±b2−4ac2asquare root method; 4a2=2∣a∣, and ± covers the signx=−b2a±b2−4ac2ax=−b±b2−4ac2a\begin{aligned} &ax^2 + bx + c = 0 \\ &x^2 + \tfrac{b}{a}x + \tfrac{c}{a} = 0 && \text{divide by } a \text{ (allowed: } a \ne 0\text{)} \\ &x^2 + \tfrac{b}{a}x = -\tfrac{c}{a} && \text{move the constant} \\ &x^2 + \tfrac{b}{a}x + \left(\tfrac{b}{2a}\right)^2 = \left(\tfrac{b}{2a}\right)^2 - \tfrac{c}{a} && \text{add } (\text{half the } x\text{-coefficient})^2 \text{ to both sides} \\ &\left(x + \tfrac{b}{2a}\right)^2 = \frac{b^2}{4a^2} - \frac{4ac}{4a^2} && \text{left side is a perfect square} \\ &\left(x + \tfrac{b}{2a}\right)^2 = \frac{b^2 - 4ac}{4a^2} \\ &x + \tfrac{b}{2a} = \pm\frac{\sqrt{b^2 - 4ac}}{2a} && \text{square root method; } \sqrt{4a^2} = 2\lvert a \rvert, \text{ and } \pm \text{ covers the sign} \\ &x = -\frac{b}{2a} \pm \frac{\sqrt{b^2 - 4ac}}{2a} \\ &x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \end{aligned}

The key step is line 4: adding (b2a)2\left(\frac{b}{2a}\right)^2 turns x2+baxx^2 + \frac{b}{a}x into the perfect square (x+b2a)2\left(x + \frac{b}{2a}\right)^2 (Special Products). After that, the square root method finishes the job.

Using it

2x2−3x−4=0:a=2, b=−3, c=−4b2−4ac=9+32=41x=3±414≈2.351 or −0.851\begin{aligned} &2x^2 - 3x - 4 = 0{:}\quad a = 2,\ b = -3,\ c = -4 \\[1ex] &b^2 - 4ac = 9 + 32 = 41 \\ &x = \frac{3 \pm \sqrt{41}}{4} \approx 2.351 \text{ or } -0.851 \end{aligned}

Watch the signs: b=−3b = -3, so −b=+3-b = +3, and −4ac=−4⋅2⋅(−4)=+32-4ac = -4 \cdot 2 \cdot (-4) = +32. Most mistakes with the formula are sign mistakes in these two places.

Tip: If the equation factors, the formula gives the same roots: for x2−5x+6=0x^2 - 5x + 6 = 0, x=5±12=3 or 2x = \frac{5 \pm \sqrt{1}}{2} = 3 \text{ or } 2. A perfect-square discriminant is the sign that factoring would have worked.

In Python

python · try it yourself
import math

def solve_quadratic(a, b, c):
    d = b * b - 4 * a * c
    if d < 0:
        return []                         # no real roots
    r = math.sqrt(d)
    return [(-b - r) / (2 * a), (-b + r) / (2 * a)]

print([round(x, 4) for x in solve_quadratic(2, -3, -4)])
print(solve_quadratic(1, -5, 6))
print(solve_quadratic(1, -4, 4))          # double root, listed twice
print(solve_quadratic(1, 0, 1))           # x² + 1 = 0
[-0.8508, 2.3508]
[2.0, 3.0]
[2.0, 2.0]
[]

When does the ball land?

A ball is kicked from 0.5 m above the ground with an upward speed of 14 m/s, so its height is h(t)=0.5+14t−4.905t2h(t) = 0.5 + 14t - 4.905t^2. It lands when h=0h = 0, a quadratic in tt with a=−4.905a = -4.905, b=14b = 14, c=0.5c = 0.5:

python · try it yourself
import math

a, b, c = -4.905, 14.0, 0.5
d = b * b - 4 * a * c
t1 = (-b + math.sqrt(d)) / (2 * a)
t2 = (-b - math.sqrt(d)) / (2 * a)
print(f"roots: {t1:.4f} s and {t2:.4f} s")
print(f"lands after {max(t1, t2):.3f} s")
print(f"highest point at t = {-b / (2 * a):.3f} s, height {c - b * b / (4 * a):.3f} m")
roots: -0.0353 s and 2.8895 s
lands after 2.890 s
highest point at t = 1.427 s, height 10.490 m

The negative root is the moment the ball "would have" left the ground if its path were extended backwards; the program keeps the positive one. The peak lies halfway between the roots, at t=−b2at = -\frac{b}{2a} (see Completing the Square for why).

In software: numerically stable roots

In floating point, the formula as written can lose almost all its accuracy. Take x2+108x+1=0x^2 + 10^8x + 1 = 0. The roots are about −108-10^8 and −10−8-10^{-8}. For the small root the formula computes −b+b2−4ac-b + \sqrt{b^2 - 4ac}, the difference of two numbers that agree in about 16 digits, and the rounding error in …\sqrt{\ldots} swamps the result. This is catastrophic cancellation.

The fix uses the fact that the product of the roots is c/ac/a (Vieta). Compute the root where −b-b and ±D\pm\sqrt{D} have the same sign, so they add instead of cancel, and get the other root by division:

q=−b+sign⁡(b)⋅b2−4ac2x1=q/ax2=c/q\begin{aligned} q &= -\frac{b + \operatorname{sign}(b) \cdot \sqrt{b^2 - 4ac}}{2} \\ x_1 &= q / a \\ x_2 &= c / q \end{aligned}
python · try it yourself
import math

def naive(a, b, c):
    r = math.sqrt(b * b - 4 * a * c)
    return (-b + r) / (2 * a), (-b - r) / (2 * a)

def stable(a, b, c):
    r = math.sqrt(b * b - 4 * a * c)
    q = -(b + math.copysign(r, b)) / 2     # same sign: no cancellation
    return c / q, q / a

a, b, c = 1.0, 1e8, 1.0
for name, f in [("naive ", naive), ("stable", stable)]:
    small, big = f(a, b, c)
    print(f"{name}: small root {small:.10e}   residual {a*small*small + b*small + c:.1e}")
naive : small root -7.4505805969e-09   residual 2.5e-01
stable: small root -1.0000000000e-08   residual 1.1e-16

The naive small root is wrong already in the first digit (−7.45×10−9-7.45 \times 10^{-9} instead of −1.00×10−8-1.00 \times 10^{-8}), while the stable version is correct to all digits shown. math.copysign(r, b) gives r with the sign of b, which is the whole trick. Numerical libraries (and numpy.roots, which uses a different, eigenvalue-based method) avoid the textbook formula for this reason; the same care matters in ray tracers, where the small root is the nearest hit (discriminant and ray tests).

Exercises

Exercise 1: Use the formula

Solve x2+4x−1=0x^2 + 4x - 1 = 0 exactly.

Answer

x=−2±5x = -2 \pm \sqrt{5} (≈0.236\approx 0.236 and −4.236-4.236)

Solution
D=16+4=20,20=25x=−4±252=−2±5\begin{aligned} &D = 16 + 4 = 20, \quad \sqrt{20} = 2\sqrt{5} \\ &x = \frac{-4 \pm 2\sqrt{5}}{2} = -2 \pm \sqrt{5} \end{aligned}

Exercise 2: Negative coefficients

Solve −3x2+2x+5=0-3x^2 + 2x + 5 = 0.

Answer

x=−1x = -1 or x=53x = \frac{5}{3}

Solution
a=−3, b=2, c=5D=4−4(−3)(5)=4+60=64,D=8x=−2±8−6→6−6=−1or−10−6=53\begin{aligned} &a = -3,\ b = 2,\ c = 5 \\ &D = 4 - 4(-3)(5) = 4 + 60 = 64, \quad \sqrt{D} = 8 \\ &x = \frac{-2 \pm 8}{-6} \quad\to\quad \frac{6}{-6} = -1 \quad\text{or}\quad \frac{-10}{-6} = \frac{5}{3} \end{aligned}

Exercise 3: Formula with fractions

Solve x22−x−32=0\frac{x^2}{2} - x - \frac{3}{2} = 0. (Hint: clear fractions first.)

Answer

x=3x = 3 or x=−1x = -1

Solution

Multiply by 2: x2−2x−3=0x^2 - 2x - 3 = 0. D=4+12=16D = 4 + 12 = 16, x=2±42=3 or −1x = \frac{2 \pm 4}{2} = 3 \text{ or } -1.

Exercise 4: Drop from a bridge

A stone is thrown down at 3 m/s from a bridge 30 m above the water: h(t)=30−3t−4.905t2h(t) = 30 - 3t - 4.905t^2. When does it hit the water? Write the code.

Answer

After about 2.19 s.

Solution
python · try it yourself
import math

a, b, c = -4.905, -3.0, 30.0
r = math.sqrt(b * b - 4 * a * c)
roots = [(-b + r) / (2 * a), (-b - r) / (2 * a)]
print([round(t, 3) for t in roots])
print("hits the water at", round(max(roots), 3), "s")
[-2.798, 2.186]
hits the water at 2.186 s

Exercise 5: Check the stable formula

Use stable(a, b, c) from the lesson on 2x2−3x−4=02x^2 - 3x - 4 = 0 and compare with the exact roots 3±414\frac{3 \pm \sqrt{41}}{4}.

Answer

Both give ≈−0.8508\approx -0.8508 and ≈2.3508\approx 2.3508; the stable formula works for any signs.

Solution
python · try it yourself
import math

def stable(a, b, c):
    r = math.sqrt(b * b - 4 * a * c)
    q = -(b + math.copysign(r, b)) / 2
    return c / q, q / a

print([round(x, 4) for x in stable(2, -3, -4)])
print(round((3 - math.sqrt(41)) / 4, 4), round((3 + math.sqrt(41)) / 4, 4))
[-0.8508, 2.3508]
-0.8508 2.3508

Test yourself

For 3x2−6x+2=03x^2 - 6x + 2 = 0, what is the discriminant b2−4acb^2 - 4ac?

  1. 60
  2. 12
  3. 36
  4. −12

Which step of completing the square produces the ±\pm in the quadratic formula?

  1. Dividing by aa
  2. Adding (b/2a)2(b/2a)^2
  3. Taking the square root of both sides
  4. Moving cc to the right

Why is the textbook formula inaccurate for x2+108x+1=0x^2 + 10^8x + 1 = 0?

  1. The discriminant is negative
  2. For the small root it subtracts two nearly equal numbers
  3. Python cannot store 10810^8
  4. aa is too small