2010-11-17 Tavian Barnes
Although a closed form solution exists for the roots of polynomials of degree ≤ 4, the general formulae for cubics (and quartics) is ugly. Various simplifications can be made; commonly, the cubic is transformed by substituting , giving
Several strategies exist for solving this depressed cubic, but most involve dealing with complex numbers, even when we only want to find real roots. Fortunately, there are a couple ways to find solutions with only real arithmetic. First, we need to know the value of the discriminant .
If , then there are three real roots; this is the tough case. Luckily, we can use the uncommon trigonometric identity to calculate the roots with trig functions. Making another substitution, , gives us
Thus the three roots are:
for . We can save a cosine calculation by noting that and . When generated in this way, the roots are ordered from smallest to largest ().
For the case, there is a single real root. We can apply the general cubic formula and avoid dealing with complex numbers, provided we choose the signs right. In this case,
When , there is a root of at least multiplicity 2, and we can avoid calculating any radicals. If , the only root is ; otherwise, the duplicate root is and the simple root is .
My C implementation of this solution method can be seen in the
dmnsn_solve_cubic() function here.