[][src]Function roots::find_roots_cubic

pub fn find_roots_cubic<F: FloatType>(a3: F, a2: F, a1: F, a0: F) -> Roots<F>

Solves a cubic equation a3x^3 + a2x^2 + a1*x + a0 = 0.

General formula (complex numbers) is implemented for three roots.

Note that very small values of a3 (comparing to other coefficients) will cause the loss of precision.

In case more than one roots are present, they are returned in the increasing order.

Examples

use roots::Roots;
use roots::find_roots_cubic;

let no_roots = find_roots_cubic(0f32, 1f32, 0f32, 1f32);
// Returns Roots::No([]) as 'x^2 + 1 = 0' has no roots

let one_root = find_roots_cubic(1f64, 0f64, 0f64, 0f64);
// Returns Roots::One([0f64]) as 'x^3 = 0' has one root 0

let three_roots = find_roots_cubic(1f32, 0f32, -1f32, 0f32);
// Returns Roots::Three([-1f32, 0f32, 1f32]) as 'x^3 - x = 0' has roots -1, 0, and 1

let three_roots_less_precision = find_roots_cubic(
           -0.000000000000000040410628481035f64,
           0.0126298310280606f64,
           -0.100896606408756f64,
           0.0689539597036461f64);
// Returns Roots::Three([0.7583841816097057f64, 7.233267996296344f64, 312537357195212.9f64])
// while online math expects 0.7547108770537f64, 7.23404258961f64, 312537357195213f64