How to use the cubic equation solver
Enter the coefficients of ax³ + bx² + cx + d = 0 and the discriminant Δ decides the shape of the answer first. Δ > 0 means three distinct real roots, Δ = 0 means a repeated root, and Δ < 0 means one real root plus a conjugate pair of complex roots. The matching method is then applied.
The work is done on the depressed cubic t³ + pt + q = 0 obtained by writing x = t − b/(3a). When all three roots are real, the classical Cardano formula requires the square root of a negative number and so passes through complex numbers. This solver avoids that by using the trigonometric form t = 2√(−p/3)·cos(θ/3 − 2πk/3). Cardano is used only when Δ < 0: one real root is found, then dividing it out leaves a quadratic that gives the conjugate pair.
Each root is substituted back into the original equation and the resulting |f(x)| is displayed. The closer that value is to zero, the more accurate the root, so the answer carries its own verification. The sum and product of the roots are also printed next to the expected values −b/a and −d/a.
Δ = 0 is never tested with an equality. In floating point arithmetic a quantity that should be exactly zero rarely lands on zero, so the test uses a relative tolerance of 1e-9 scaled to the size of the coefficients. If a is 0 the equation is not cubic and a message says so. Quartic and higher equations are not supported.
Frequently asked questions
Δ > 0 gives three distinct real roots, Δ = 0 means a repeated root, and Δ < 0 gives one real root with a conjugate pair of complex roots. You know the shape before solving.
With three real roots the Cardano formula needs the square root of a negative number, so it cannot be evaluated in real arithmetic. The trigonometric form is used instead.
It is the largest |f(x)| obtained by substituting the roots back into the equation. A value far below 1e-6 means the roots are accurate.