Advertisement
pVinc

Untitled

May 11th, 2021
570
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cmath>
  2. #include <complex>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. #define sqrt3 (sqrt(3.0) / 2.0)
  8.  
  9. int main(int argc, char **argv) {
  10.   double a, b, c, d, D, p, q, u, v;
  11.   complex<double> z1, z2, z3;
  12.   cin >> a;
  13.   cin >> b;
  14.   cin >> c;
  15.   cin >> d;
  16.  
  17.   q = ((2.0 * pow(b, 3.0)) / (27 * pow(a, 3.0))) -
  18.       ((b * c) / (3 * pow(a, 2.0))) + (d / a);
  19.   p = (c / a) - (pow(b, 2.0) / (3.0 * pow(a, 2.0)));
  20.   D = pow(q / 2.0, 2.0) + pow(p / 3.0, 3.0);
  21.  
  22.   cout << "p = " << p << endl << "q = " << q << endl;
  23.  
  24.   if (D >= 0) {
  25.     u = pow(-(q / 2) + sqrt(abs(D)), 1.0 / 3.0);
  26.     v = -pow(abs(-(q / 2) - sqrt(abs(D))), 1.0 / 3.0);
  27.  
  28.     z1 = u + v - (b / (3 * a));
  29.     z2 = (-0.5 * (u + v) - (b / (3 * a))) + (1i * sqrt3 * (u - v));
  30.     z3 = (-0.5 * (u + v) - (b / (3 * a))) - (1i * sqrt3 * (u - v));
  31.     cout << z1 << endl;
  32.     cout << z2 << endl;
  33.     cout << z3 << endl;
  34.   } else {
  35.     double phi = acos((-0.5 * q) / sqrt(-((p * p * p) / 27)));
  36.     z1 = 2 * sqrt(-p / 3) * cos(phi / 3) - (b / (3 * a));
  37.     z2 = 2 * sqrt(-p / 3) * cos((phi + (2 * M_PI)) / 3) - (b / (3 * a));
  38.     z3 = 2 * sqrt(-p / 3) * cos((phi + (4 * M_PI)) / 3) - (b / (3 * a));
  39.     cout << z1 << endl;
  40.     cout << z2 << endl;
  41.     cout << z3 << endl;
  42.   }
  43.   return 0;
  44. }
  45.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement