problem with cubic equation

Jan 29 2009 5:49 PM
guyz this is my C# programm for solving cubic eqation not tottaly build but i am facing a prob with code in  else if (h > 0) {code} the value of u is showing as NaN can plzz help with this

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            double a = Convert.ToInt32 (Console.ReadLine());
            double b = Convert.ToInt32(Console.ReadLine());
            double c = Convert.ToInt32(Console.ReadLine());
            double d = Convert.ToInt32(Console.ReadLine());
            double e,j,l,x1,x2,x3,k,m,n,p;
            double r,s,t,u;
            double f = (((3 * c) / a) - (((b * b) / (a * a)))) / 3;
            double g = ((2 * ((b * b * b) / (a * a * a)) - (9 * b * c / (a * a)) + ((27 * (d / a))))) / 27;
            double h = (((g * g) / 4) + ((f * f * f) / 27));
            if (h < 0)
            {
                Console.WriteLine("all the three roots are real");
                e = (Math.Sqrt((g * g / 4) - h));               
                j =Math.Pow(e,(0.3));                 
                k =Math.Acos((-g/(2*e)));
                l = j * -1;
                m = Math.Cos(k / 3);
                n = Math.Sqrt(3) * (Math.Sin(k / 3));
                p = (b / (3 * a)) * -1;
                x1 = (2 * (j * Math.Cos(k / 3)) - (b / (3 * a)));
                x2 = l * (m + n) + p;
                x3 = l * (m - n) + p;
               

                Console.WriteLine("{0},{1},{2}",x1,x2,x3);
                 

            }
            else if (h > 0)
            {
                Console.WriteLine("Only one root is real");
                r = (-((g) / 2)) + (Math.Sqrt(h));
                s = Math.Pow(r, (0.3));
                t = (-((g) / 2)) - (Math.Sqrt(h));
                u = Math.Pow(t, (0.3));
                x1 = ((s + u) - (b / (3 * a)));
                Console.WriteLine(x1);
            }
            else if ((h + g + f) == 0)
            {
                x1 = x2 = x3 = Math.Pow((d / a), 0.3) * (-1);
                Console.WriteLine("{0},{1},{2}", x1, x2, x3);
            }

        }
    }
}
 thanx
Aimee

Answers (1)