Advertisement

What is your favorite quadratic equation solution?

Started by May 20, 2001 04:02 PM
1 comment, last by Succinct 23 years, 8 months ago
what's ur guys solutions to the quadratic equation? here's my c++ one:
      
// the quadratic equation

//===============================================================

struct QuadraticCoeffecients
{
    float a,b,c;
};
//---------------------------------------------------------------

struct SolvedQuadratic
{
    int   NumRootsFound;
    float Root[2];
};
//---------------------------------------------------------------

SolvedQuadratic QuadraticEquation( const QuadraticCoeffecients& q )
{
    float Discriminant,t1,t2;  // found values

    SolvedQuadratic s;         // solution


    float a = q.a;  // alias the structure's variables

    float b = q.b;  //

    float c = q.c;  //


    Discriminant = b*b - 4*a*c;
    if( Discriminant > 0 )
    {
        Discriminant = Sqrt( Discriminant );
        t1 = (-b - Discriminant)/(2*a);
        t2 = (-b + Discriminant)/(2*a);

        if( t1 > t2 )
        {
            if( t1 < 0 )
            {
                s.NumRootsFound = 0;
                return s; 
            }
            else    // t1 >= 0

            {
                if( t2 < 0 )
                {
                    s.NumRootsFound = 1;
                    s.Root[0] = t1;
                    return s; 
                }
                else    // t2 >= 0

                {
                    s.NumRootsFound = 2;
                    s.Root[0] = t1;
                    s.Root[1] = t2;
                    return s; 
                }
            }
        }
        else    // t2 >= t1

        {
            if( t2 < 0 )
            {
                s.NumRootsFound = 0;
                return s; 
            }
            else    // t2 >= 0

            {
                if( t1 < 0 )
                {
                    s.NumRootsFound = 1;
                    s.Root[0] = t2;
                    return s; 
                }
                else
                {
                    s.NumRootsFound = 2;
                    s.Root[0] = t2;
                    s.Root[1] = t1;
                    return s; 
                }
            }
        }
    }
    else    // Discriminant <= 0

        if( Discriminant == 0 )
        {
            s.NumRootsFound = 1;
            s.Root[0] = -b/2;
            return s; 
        }

    s.NumRootsFound = 0;
    return s;
}
  
Thank you for your bandwidth. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~Succinct Demos Online~ Edited by - Succinct on May 20, 2001 5:09:33 PM
-- Succinct(Don't listen to me)
    struct CComplex{float r, i;//pile of overloads...};int QuadraticRoots(CComplex coef[3], CComplex roots[2]){static float fEplison = 0.00001;CComplex a = coef[2];CComplex b = coef[1];CComplex c = coef[0];if(abs(a) < fEplison)	{	//They we're just kidding	return LinearRoots(coef, roots);	}CComplex D = sqrt(b*b - 4*a*c);CComplex negb = -b;CComplex twicea = 2*a;roots[0] = (negb + D) / twicea;roots[1] = (negb - D) / twicea;return(2);}        


Magmai Kai Holmlor
- The disgruntled & disillusioned


Edited by - Magmai Kai Holmlor on May 20, 2001 6:48:43 PM

Edited by - Magmai Kai Holmlor on May 20, 2001 6:50:40 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
quote:
Original post by Magmai Kai Holmlor
//They we''re just kidding



LOL!

This topic is closed to new replies.

Advertisement