title: Problem with calculating cubic equations in C#
body: <p>Recently I was working on a discord bot to compute the roots of a cubic equation.</p>
  <p>I have already made another program beforehand in C# that basically does the same thing, and is <strong>proven to work correctly,</strong> so I straight up copied the codes into the one which I am working with now.</p>
  <p><em><strong>(Suppose that I am calculating the roots of 7x<sup>3</sup> + 2x<sup>2</sup> + 12x + 9 = 0)</strong></em><br />
  The code below is from the program which was created beforehand:<br />
  <em>(Once again, the program and the results</em> <strong>are proven to be correct</strong>, <em>results are listed at the bottom of the code)</em>*</p>
  <pre><code>// parts 1-5
  double p1 = -(b / (3 * a));
  double p2 = (b * c / (6 * Math.Pow(a, 2))) - (Math.Pow(b, 3) / (27 * Math.Pow(a, 3))) - (d / (2 * a));
  double p3 = (c / (3 * a)) - (Math.Pow(b, 2) / (9 * Math.Pow(a, 2)));
  Complex p4 = new Complex(-1.0 / 2, Math.Sqrt(3) / 2);
  Complex p5 = new Complex(-1.0 / 2, -(Math.Sqrt(3) / 2));
  
  // solve for roots
  Complex root1 = p1 + Math.Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))) +
                   Math.Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)));
  Complex root2 = p1 + (p4 * Math.Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)))) +
                   (p5 * Math.Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))));
  Complex root3 = p1 + (p5 * Math.Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)))) +
                   (p4 * Math.Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))));
  /* 
  results :
  root1 = (-0.6566823203779361, 0)
  root2 = (0.18548401733182518, 1.3868992549529795)
  root3 = (0.18548401733182518, -1.3868992549529795)
  */
  </code></pre>
  <p>I proceed to copy the whole thing into the new program I am working on. Then I realized in my new program the Math.Cbrt function doesn't exist <em>(I don't know why)</em>. Therefore I made a custom program to tackle this problem, here is the code in my new program :<br />
  <em>(This is the problematic program, the results are different from the first one's.)</em></p>
  <pre><code>// parts 1-5
  double p1 = -(b / (3 * a));
  double p2 = (b * c / (6 * Math.Pow(a, 2))) - (Math.Pow(b, 3) / (27 * Math.Pow(a, 3))) - (d / (2 * a));
  double p3 = (c / (3 * a)) - (Math.Pow(b, 2) / (9 * Math.Pow(a, 2)));
  Complex p4 = new Complex(-1.0 / 2, Math.Sqrt(3.0) / 2);
  Complex p5 = new Complex(-1.0 / 2, -(Math.Sqrt(3.0) / 2));
  
  // solve for roots
  Complex root1 = p1 + Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))) +
       Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)));
  Complex root2 = p1 + (p4 * Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)))) +
                   (p5 * Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))));
  Complex root3 = p1 + (p5 * Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)))) +
                   (p4 * Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))));
  
  public static Complex Cbrt(Complex value)
  {
      Complex result = Complex.Pow(value, 1D / 3);
      return result;
  }
  /* 
  results :
  root1 = (0.965490835755936, 0.936562108366076)
  root2 = (0.185484017331825, -0.486224961779172)
  root3 = (-1.43668913880205, -0.450337146586904)
  */
  </code></pre>
  <p>I have tried to extract different sections of the codes and run it separately but I still wasn't able to find what the cause is. If the codes are the same, why do they yield different results? Or is the Cbrt method I have created the real problem behind this?</p>