TExcelTAD
Home › Algebra › The Discriminant and Complex Roots

The Discriminant and Complex Roots

The expression under the square root in the quadratic formula, D=b2−4acD = b^2 - 4ac, 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, x=−b±D2ax = \frac{-b \pm \sqrt{D}}{2a}:

Discriminant Roots Parabola y=ax2+bx+cy = ax^2 + bx + c
D>0D > 0 two different real roots crosses the x-axis twice
D=0D = 0 one repeated real root, x=−b2ax = -\frac{b}{2a} touches the x-axis at its vertex
D<0D < 0 no real roots; two complex conjugate roots misses the x-axis

For equations with integer coefficients there is one more distinction: if DD is a perfect square, the roots are rational and the trinomial factors over the integers.

x2−6x+5:D=36−20=16>0, a perfect square→ two rational roots (1 and 5)x2−6x+7:D=36−28=8>0→ two irrational roots (3±2)x2−6x+9:D=36−36=0→ one root (3, double)x2−6x+13:D=36−52=−16<0→ no real roots\begin{aligned} x^2 - 6x + 5{:}\quad &D = 36 - 20 = 16 > 0, \text{ a perfect square} && \to\ \text{two rational roots (1 and 5)} \\ x^2 - 6x + 7{:}\quad &D = 36 - 28 = 8 > 0 && \to\ \text{two irrational roots } (3 \pm \sqrt{2}) \\ x^2 - 6x + 9{:}\quad &D = 36 - 36 = 0 && \to\ \text{one root (3, double)} \\ x^2 - 6x + 13{:}\quad &D = 36 - 52 = -16 < 0 && \to\ \text{no real roots} \end{aligned}

Complex numbers in one paragraph

No real number squares to a negative, so mathematicians introduced the imaginary unit ii with i2=−1i^2 = -1. Then −16=16⋅−1=4i\sqrt{-16} = \sqrt{16} \cdot \sqrt{-1} = 4i. A complex number has the form a+bia + bi with real aa (the real part) and bb (the imaginary part). With them, every quadratic has exactly two roots (counting a double root twice), and more generally every polynomial of degree nn has nn roots: the Fundamental Theorem of Algebra. A fuller introduction is in Complex Numbers.

x2−6x+13=0x=6±−162=6±4i2=3±2icheck x=3+2i:(3+2i)2=9+12i+4i2=5+12icheck x=3+2i:5+12i−6(3+2i)+13=5+12i−18−12i+13=0 ✓\begin{aligned} &x^2 - 6x + 13 = 0 \\ &x = \frac{6 \pm \sqrt{-16}}{2} = \frac{6 \pm 4i}{2} = 3 \pm 2i \\ &\text{check } x = 3 + 2i{:}\quad (3 + 2i)^2 = 9 + 12i + 4i^2 = 5 + 12i \\ &\phantom{\text{check } x = 3 + 2i{:}}\quad 5 + 12i - 6(3 + 2i) + 13 = 5 + 12i - 18 - 12i + 13 = 0\ \checkmark \end{aligned}

When aa, bb, cc are real, complex roots always come as a conjugate pair p+qip + qi and p−qip - qi: the ±\pm in the formula only flips the sign of the imaginary part.

Complex roots in Python

Python writes ii as j (the electrical-engineering convention) and has complex numbers built in. math.sqrt(-16) raises an error, but cmath.sqrt(-16) returns 4j:

python · try it yourself
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 ii as I and keeps everything exact; numpy.roots returns complex floats:

python · try it yourself
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 OO in direction dd: the points P(t)=O+t⋅dP(t) = O + t \cdot d for t≥0t \ge 0. A sphere with centre CC and radius rr is all points with ∣P−C∣2=r2\lvert P - C \rvert^2 = r^2. Substituting the ray gives a quadratic in tt:

(d⋅d) t2+2 (d⋅(O−C)) t+(O−C)⋅(O−C)−r2=0(d \cdot d)\, t^2 + 2\,(d \cdot (O - C))\, t + (O - C) \cdot (O - C) - r^2 = 0

The discriminant answers the question "does the ray hit the sphere?" before any square root is taken: D<0D < 0 is a miss, D=0D = 0 grazes the edge, D>0D > 0 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).

python · try it yourself
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 (D<0D < 0).

Exercises

Exercise 1: Count the roots

Use the discriminant to find how many real roots each has: (a) 3x2−2x+5=03x^2 - 2x + 5 = 0 (b) 4x2+12x+9=04x^2 + 12x + 9 = 0 (c) x2−3x−1=0x^2 - 3x - 1 = 0

Answer

(a) none (D=−56D = -56); (b) one (D=0D = 0); (c) two (D=13D = 13).

Solution
(a)4−60=−56(b)144−144=0(it is (2x+3)2)(c)9+4=13\begin{aligned} &\text{(a)}\quad 4 - 60 = -56 \\ &\text{(b)}\quad 144 - 144 = 0 && \text{(it is } (2x + 3)^2\text{)} \\ &\text{(c)}\quad 9 + 4 = 13 \end{aligned}

Exercise 2: Complex roots by hand

Solve x2+4x+13=0x^2 + 4x + 13 = 0.

Answer

x=−2±3ix = -2 \pm 3i

Solution
D=16−52=−36,−36=6ix=−4±6i2=−2±3i\begin{aligned} &D = 16 - 52 = -36, \quad \sqrt{-36} = 6i \\ &x = \frac{-4 \pm 6i}{2} = -2 \pm 3i \end{aligned}
python · try it yourself
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 kk for a double root

For which values of kk does x2+kx+16=0x^2 + kx + 16 = 0 have exactly one real root?

Answer

k=8k = 8 or k=−8k = -8

Solution
D=k2−64=0→k2=64→k=±8k=8: (x+4)2=0;k=−8: (x−4)2=0\begin{aligned} &D = k^2 - 64 = 0 \quad\to\quad k^2 = 64 \quad\to\quad k = \pm 8 \\ &k = 8{:}\ (x + 4)^2 = 0; \quad k = -8{:}\ (x - 4)^2 = 0 \end{aligned}

Exercise 4: Can the ball reach 12 m?

A ball's height is h(t)=1+14t−4.905t2h(t) = 1 + 14t - 4.905t^2. Use the discriminant of h(t)=12h(t) = 12 to decide whether it ever reaches 12 m, and 10 m.

Answer

It reaches 10 m (D>0D > 0) but not 12 m (D<0D < 0).

Solution

h(t)=Hh(t) = H means −4.905t2+14t+(1−H)=0-4.905t^2 + 14t + (1 - H) = 0, with D=142+4⋅4.905⋅(1−H)D = 14^2 + 4 \cdot 4.905 \cdot (1 - H).

python · try it yourself
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 2x2+x+3=02x^2 + x + 3 = 0 add to −b/a-b/a and multiply to c/ac/a, using Python's complex numbers.

Answer

Sum −0.5=−12-0.5 = -\frac{1}{2}, product 1.5=321.5 = \frac{3}{2}, both with zero imaginary part.

Solution
python · try it yourself
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 b2−4ac<0b^2 - 4ac < 0 (with real a,b,ca, b, c), the quadratic has…

  1. two real roots
  2. one real root
  3. two complex conjugate roots
  4. no roots at all

What does Python's cmath.sqrt(-9) return?

  1. 3j
  2. -3
  3. an error
  4. nan

In a ray–sphere test, the discriminant is exactly 0. What does that mean?

  1. The ray misses
  2. The ray passes through the centre
  3. The ray just touches the sphere at one point
  4. The sphere has radius 0