Advertisement
artemgf

Функция подсчёта корней кубического уравнения

Jan 8th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. int Cubic(double a, double b, double c,double d) {
  2.     double p = ((3.0*a*c - b*b) / (3.0*a*a));
  3.     double q = ((2.0*b*b*b - 9.0*a*b*c + 27.0*a*a*d))/(27.0*a*a*a);
  4.     double h = ((p*p*p) / 27.0);
  5.     double j = (q*q) / 4.0;
  6.     double s = (4 * (3 * a*c - b*b)*(3 * a*c - b*b)*(3 * a*c - b*b) + (2 * b*b*b - 9 * a*b*c + 27 * a*a*d)*(2 * b*b*b - 9 * a*b*c + 27 * a*a*d))/ (2916 * a*a*a*a*a*a);
  7.     if (s < 0)
  8.         {
  9.             double f;
  10.             if (q < 0)
  11.             {
  12.                 f = atan(sqrt(-s) / (-q / 2));
  13.             }
  14.             else if (q > 0)
  15.             {
  16.                 f = atan(sqrt(-s) / (-q / 2)) + M_PI;
  17.             }
  18.             else
  19.             {
  20.                 f = (M_PI / 2);
  21.             }
  22.             double k;
  23.             if (p == 0)
  24.                 k = 0;
  25.             else
  26.                 k = sqrt(-p / 3.0);
  27.             double x1 = (2.0*k*cos(f / 3.0) - b / 3.0*a);
  28.             double x2 = (2.0*k*cos(f / 3.0 + (2.0*M_PI) / 3.0) - b / 3.0*a);
  29.             double x3 = (2.0*k*cos(f / 3.0 + (4.0*M_PI) / 3.0) - b / 3.0*a);
  30.             if (x1 != x2&&x1 != x3&&x2 != x3)
  31.                 return 1;
  32.             else
  33.                 return 2;
  34.         }
  35.         else
  36.             if (s > 0)
  37.             {
  38.                 double mi;
  39.                 double pl;
  40.                 double mic;
  41.                 double plc;
  42.                 mi = -q / 2.0 - sqrt(s);
  43.                 pl = -q / 2.0 + sqrt(s);
  44.                 if (pl == 0)
  45.                     plc = 0;
  46.                 else
  47.                     plc = pow(abs(pl), 1 / 3.0)*(abs(pl) / pl);
  48.                 if (mi == 0)
  49.                     mic = 0;
  50.                 else
  51.                     mic = pow(abs(mi), 1 / 3.0)*(abs(mi) / mi);
  52.                 double x1 = plc + mic - (b / (3.0*a));
  53.                 double x2 = (-1 / 2.0)*(plc + mic) - (b / (3.0*a));
  54.                 double x2i = (sqrt(3.0) / 2.0)*(plc - mic);
  55.                 double x3 = (-1 / 2.0)*(plc + mic) - (b / (3.0*a));
  56.                 double x3i = -(sqrt(3.0) / 2.0)*(plc - mic);
  57.                 if (x3i != x2i&&x2 != x3)
  58.                     return 2;
  59.                 else
  60.                     return 1;
  61.             }
  62.             else
  63.             {
  64.                 double k = -q / 2.0;
  65.                 double f;
  66.                 if (k == 0)
  67.                     f = 0;
  68.                 else
  69.                     f = pow(abs(k), 1 / 3.0)*(abs(k) / k);
  70.                 double x1 = (2.0*f - (b / (3.0*a)));
  71.                 double x2 = (-1.0*f - (b / (3.0*a)));
  72.                 double x3 = (-1.0*f - (b / (3.0*a)));
  73.                 if (x1 != x2&&x1 != x3&&x2 != x3)
  74.                     return 1;
  75.                 else
  76.                     return 2;
  77.             }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement