The Discriminant and Complex Roots
The expression under the square root in the quadratic formula, , is called the discriminant. Without solving anything, its sign tells you how many real roots a quadratic has. When it is negative, the roots are complex numbers, which Python handles with its built-in complex type and the cmath module. In graphics, the same test decides whether a ray hits a sphere.
Three cases
From the quadratic formula, :
| Discriminant | Roots | Parabola |
|---|---|---|
| two different real roots | crosses the x-axis twice | |
| one repeated real root, | touches the x-axis at its vertex | |
| no real roots; two complex conjugate roots | misses the x-axis |
For equations with integer coefficients there is one more distinction: if is a perfect square, the roots are rational and the trinomial factors over the integers.
Complex numbers in one paragraph
No real number squares to a negative, so mathematicians introduced the imaginary unit with . Then . A complex number has the form with real (the real part) and (the imaginary part). With them, every quadratic has exactly two roots (counting a double root twice), and more generally every polynomial of degree has roots: the Fundamental Theorem of Algebra. A fuller introduction is in Complex Numbers.
When , , are real, complex roots always come as a conjugate pair and : the in the formula only flips the sign of the imaginary part.
Complex roots in Python
Python writes as j (the electrical-engineering convention) and has complex numbers built in. math.sqrt(-16) raises an error, but cmath.sqrt(-16) returns 4j:
import cmath
import math
def roots(a, b, c):
d = b * b - 4 * a * c
r = cmath.sqrt(d) # works for negative d too
return d, (-b + r) / (2 * a), (-b - r) / (2 * a)
for a, b, c in [(1, -6, 5), (1, -6, 9), (1, -6, 13), (2, 1, 3)]:
d, x1, x2 = roots(a, b, c)
print(f"D = {d:4d}: {x1:.4f} {x2:.4f}")
try:
math.sqrt(-16)
except ValueError as e:
print("math.sqrt(-16):", e)
z = 3 + 2j
print("check:", z * z - 6 * z + 13, " real", z.real, " imag", z.imag, " conjugate", z.conjugate())D = 16: 5.0000+0.0000j 1.0000+0.0000j
D = 0: 3.0000+0.0000j 3.0000+0.0000j
D = -16: 3.0000+2.0000j 3.0000-2.0000j
D = -23: -0.2500+1.1990j -0.2500-1.1990j
math.sqrt(-16): math domain error
check: 0j real 3.0 imag 2.0 conjugate (3-2j)
SymPy writes as I and keeps everything exact; numpy.roots returns complex floats:
import numpy as np
import sympy as sp
x = sp.symbols("x")
print(sp.solve(x**2 - 6*x + 13, x))
print(sp.solve(2*x**2 + x + 3, x))
print(sp.discriminant(x**2 - 6*x + 13, x))
print([complex(round(r.real, 4), round(r.imag, 4)) for r in np.roots([1, -6, 13])])[3 - 2*I, 3 + 2*I]
[-1/4 - sqrt(23)*I/4, -1/4 + sqrt(23)*I/4]
-16
[(3+2j), (3-2j)]
In software: ray–sphere intersection
A ray tracer shoots a ray from the camera at in direction : the points for . A sphere with centre and radius is all points with . Substituting the ray gives a quadratic in :
The discriminant answers the question "does the ray hit the sphere?" before any square root is taken: is a miss, grazes the edge, enters and exits, and the smaller positive root is the visible hit. This test runs millions of times per frame in ray tracers and in game collision code (Collision Timing with Quadratics, dot product).
import math
def hit_sphere(origin, direction, centre, radius):
oc = [o - c for o, c in zip(origin, centre)]
a = sum(d * d for d in direction)
b = 2 * sum(d * v for d, v in zip(direction, oc))
c = sum(v * v for v in oc) - radius * radius
D = b * b - 4 * a * c
if D < 0:
return round(D, 4), None
t = (-b - math.sqrt(D)) / (2 * a) # nearer intersection
return round(D, 4), round(t, 4)
centre, radius = (0, 0, -5), 1
for direction in [(0, 0, -1), (0.2, 0, -1), (0.5, 0, -1)]:
print(direction, hit_sphere((0, 0, 0), direction, centre, radius))(0, 0, -1) (4, 4.0)
(0.2, 0, -1) (0.16, 4.6154)
(0.5, 0, -1) (-20.0, None)
The first ray goes straight at the centre and hits at distance 4 (the front of the sphere); the second, aimed off-centre, hits with a much smaller discriminant (it nearly grazes the edge); the third misses ().
Exercises
Exercise 1: Count the roots
Use the discriminant to find how many real roots each has: (a) (b) (c)
Answer
(a) none (); (b) one (); (c) two ().
Solution
Exercise 2: Complex roots by hand
Solve .
Answer
Solution
import cmath
a, b, c = 1, 4, 13
r = cmath.sqrt(b * b - 4 * a * c)
print((-b + r) / (2 * a), (-b - r) / (2 * a))(-2+3j) (-2-3j)
Exercise 3: Find for a double root
For which values of does have exactly one real root?
Answer
or
Solution
Exercise 4: Can the ball reach 12 m?
A ball's height is . Use the discriminant of to decide whether it ever reaches 12 m, and 10 m.
Answer
It reaches 10 m () but not 12 m ().
Solution
means , with .
a, b = -4.905, 14.0
for H in [10, 12]:
c = 1 - H
D = b * b - 4 * a * c
print(H, "m: D =", round(D, 2), "->", "reached" if D >= 0 else "never reached")
print("peak height:", round(1 + b * b / (4 * 4.905), 3), "m")10 m: D = 19.42 -> reached
12 m: D = -19.82 -> never reached
peak height: 10.99 m
Exercise 5: Conjugate check
Show that the two roots of add to and multiply to , using Python's complex numbers.
Answer
Sum , product , both with zero imaginary part.
Solution
import cmath
a, b, c = 2, 1, 3
r = cmath.sqrt(b * b - 4 * a * c)
x1, x2 = (-b + r) / (2 * a), (-b - r) / (2 * a)
print(f"sum: {x1 + x2:.4f} product: {x1 * x2:.4f}")
print("-b/a =", -b / a, " c/a =", c / a)sum: -0.5000+0.0000j product: 1.5000+0.0000j
-b/a = -0.5 c/a = 1.5
The imaginary parts cancel because the roots are conjugates.
Test yourself
If (with real ), the quadratic has…
- two real roots
- one real root
- two complex conjugate roots
- no roots at all
What does Python's cmath.sqrt(-9) return?
- 3j
- -3
- an error
- nan
In a ray–sphere test, the discriminant is exactly 0. What does that mean?
- The ray misses
- The ray passes through the centre
- The ray just touches the sphere at one point
- The sphere has radius 0