in reply to solve cubic equations

It's always fun to implement numerical algorithms, especially when a closed form solution exists. You're missing some roots, though... The fundamental theorem of algebra shows that a polynomial of degree n has exactly n roots, although some (or all) of them can be complex. Your cubic solver finds only the real roots.

By the way, for finding the roots of polynomials of higher degree, there's an iterative solver on CPAN (Math::Polynomial::Solve). You'll need it if you go higher than degree 5.

Replies are listed 'Best First'.
Re^2: solve cubic equations
by no_slogan (Deacon) on May 03, 2017 at 13:43 UTC
    Quite so. If you want the complex roots, use $t multiplied by the three cube roots of unity (1 and -1/2 ± i*sqrt(3)/2) in the first if branch.