function cuberoot ( x ) { var y = Math . pow ( Math . abs ( x ) , 1 / 3 ) ; return x < 0 ? - y : y ; } function solveCubic ( a , b , c , d ) { if ( Math . abs ( a ) < 1e-8 ) { a = b ; b = c ; c = d ; if ( Math . abs ( a ) < 1e-8 ) { a = b ; b = c ; if ( Math . abs ( a ) < 1e-8 ) return [ ] ; return [ - b / a ] ; } var D = b * b - 4 * a * c ; if ( Math . abs ( D ) < 1e-8 ) return [ - b / ( 2 * a ) ] ; else if ( D > 0 ) return [ ( - b + Math . sqrt ( D ) ) / ( 2 * a ) , ( - b - Math . sqrt ( D ) ) / ( 2 * a ) ] ; return [ ] ; } var p = ( 3 * a * c - b * b ) / ( 3 * a * a ) ; var q = ( 2 * b * b * b - 9 * a * b * c + 27 * a * a * d ) / ( 27 * a * a * a ) ; var roots ; if ( Math . abs ( p ) < 1e-8 ) { roots = [ cuberoot ( - q ) ] ; } else if ( Math . abs ( q ) < 1e-8 ) { roots = [ 0 ] . concat ( p < 0 ? [ Math . sqrt ( - p ) , - Math . sqrt ( - p ) ] : [ ] ) ; } else { var D = ( q * q ) / 4 + ( p * p * p ) / 27 ; if ( Math . abs ( D ) < 1e-8 ) { roots = [ ( - 1.5 * q ) / p , ( 3 * q ) / p ] ; } else if ( D > 0 ) { var u = cuberoot ( - q / 2 - Math . sqrt ( D ) ) ; roots = [ u - p / ( 3 * u ) ] ; } else { var u = 2 * Math . sqrt ( - p / 3 ) ; var t = Math . acos ( ( 3 * q ) / p / u ) / 3 ; var k = ( 2 * Math . PI ) / 3 ; roots = [ u * Math . cos ( t ) , u * Math . cos ( t - k ) , u * Math . cos ( t - 2 * k ) ] ; } } for ( var i = 0 ; i < roots . length ; i ++ ) roots [ i ] -= b / ( 3 * a ) ; return roots ; } export default solveCubic ;