import java.lang.Math;
import java.util.Scanner;
import java.io.*;
class Complex {
double real;
double imag;
Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
// real part
public double real() {
return real;
}
// imaginary part
public double imag() {
return imag;
}
// addition
public Complex add(Complex cpx) {
return new Complex(this.real + cpx.real(), this.imag + cpx.imag());
}
// subtraction
public Complex sub(Complex cpx) {
return new Complex(this.real - cpx.real(), this.imag - cpx.imag());
}
// multiplication
public Complex mul(Complex cpx) {
return new Complex(this.real * cpx.real() - this.imag * cpx.imag(), this.real * cpx.imag() + this.imag * cpx.real());
}
// TODO: implement division
// absolute value
public double abs() {
return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2));
}
// converts complex number to string
public String toString() {
if (this.real != 0 && this.imag > 0) {
return this.real +" + "+ this.imag +"i";
}
if (this.real != 0 && this.imag < 0) {
return this.real +" - "+ Math.abs(this.imag) +"i";
}
// returns only real part
if (this.imag == 0) {
return String.valueOf(this.real);
}
// returns only imaginary part
if (this.real == 0) {
return this.imag +"i";
}
return this.real +" + "+ this.imag +"i";
}
}
public class CubicEquation {
// solves the cubic equation ax^3 + bx^2 + cx + d = 0 with cardano's method
public static Complex[] solve(double a, double b, double c, double d) {
// x^3 + rx^2 + sx + t = 0
double r = b / a;
double s = c / a;
double t = d / a;
// y^3 + py + q = 0
double p = s - Math.pow(r, 2) / 3;
double q = 2 * Math.pow(r, 3) / 27 - s * r / 3 + t;
// discriminant
double discr = Math.pow((q / 2), 2) + Math.pow((p / 3), 3);
// array for results
Complex[] res = new Complex[3];
double u = Math.pow(-q / 2.0 + Math.sqrt(discr), (1.0 / 3.0));
double v = Math.pow(-q / 2.0 - Math.sqrt(discr), (1.0 / 3.0));
// WHY IS THIS NAN HRRRRR
System.out.println(v);
if (d > 0) {
double y1 = u + v;
Complex y2 = new Complex(-(u + v) / 2, ((u - v) / 2) * Math.sqrt(3));
Complex y3 = new Complex(-(u + v) / 2, -((u - v) / 2) * Math.sqrt(3));
double x1 = y1 - r / 3;
Complex x2 = new Complex(y2.real() - r / 3, y2.imag());
Complex x3 = new Complex(y3.real() - r / 3, y3.imag());
res[0] = new Complex(x1, 0);
res[1] = x2;
res[2] = x3;
}
if (d == 0) {
double y1 = u + v;
double y2 = -u;
double y3 = -v;
res[0] = new Complex(y1, 0);
res[1] = new Complex(y2, 0);
res[2] = new Complex(y3, 0);
}
if (d < 0) {
// casus irreducibilis
}
return res;
}
public static void main(String[] args) {
}
}