Home > Enterprise >  Problem with calculating cubic equations in C#
Problem with calculating cubic equations in C#

Time:03-21

Recently I was working on a discord bot to compute the roots of a cubic equation.

I have already made another program beforehand in C# that basically does the same thing, and is proven to work correctly, so I straight up copied the codes into the one which I am working with now.

(Suppose that I am calculating the roots of 7x3 2x2 12x 9 = 0)
The code below is from the program which was created beforehand:
(Once again, the program and the results are proven to be correct, results are listed at the bottom of the 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)
*/

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 (I don't know why). Therefore I made a custom program to tackle this problem, here is the code in my new program :
(This is the problematic program, the results are different from the first one's.)

// 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)
*/

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?

CodePudding user response:

Refering @mikuszefski 's comment,

The real cause behind this is that the Cbrt() function I have created calculates negative numbers wrongly.

For example, Cbrt(-1) should yield the result -1.
However, the answer I have gotten instead is (NaN, 0).
I have found the solution in another thread.

  • Related