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 with :
The packs two answers into one line: one with , one with . The expression under the root, , 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:
The key step is line 4: adding turns into the perfect square (Special Products). After that, the square root method finishes the job.
Using it
Watch the signs: , so , and . Most mistakes with the formula are sign mistakes in these two places.
Tip: If the equation factors, the formula gives the same roots: for , . A perfect-square discriminant is the sign that factoring would have worked.
In Python
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 . It lands when , a quadratic in with , , :
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 (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 . The roots are about and . For the small root the formula computes , the difference of two numbers that agree in about 16 digits, and the rounding error in swamps the result. This is catastrophic cancellation.
The fix uses the fact that the product of the roots is (Vieta). Compute the root where and have the same sign, so they add instead of cancel, and get the other root by division:
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 ( instead of ), 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 exactly.
Answer
( and )
Solution
Exercise 2: Negative coefficients
Solve .
Answer
or
Solution
Exercise 3: Formula with fractions
Solve . (Hint: clear fractions first.)
Answer
or
Solution
Multiply by 2: . , .
Exercise 4: Drop from a bridge
A stone is thrown down at 3 m/s from a bridge 30 m above the water: . When does it hit the water? Write the code.
Answer
After about 2.19 s.
Solution
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 and compare with the exact roots .
Answer
Both give and ; the stable formula works for any signs.
Solution
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 , what is the discriminant ?
- 60
- 12
- 36
- −12
Which step of completing the square produces the in the quadratic formula?
- Dividing by
- Adding
- Taking the square root of both sides
- Moving to the right
Why is the textbook formula inaccurate for ?
- The discriminant is negative
- For the small root it subtracts two nearly equal numbers
- Python cannot store
- is too small