I'm not sure if I remember how to do the Quadratic Formula because I haven't had to use it since I learned about it back in '05. So I did a little research on how the formula works and from what I understand the formula is:

//      -b +- sqrt(Δ)
            // x = ---------------
            //            2a
            //
            // Where Δ = b^2 - 4ac

So I decided to play around for a little bit and decided to make a method that would solve it for me, and this is what I came up with for an algorithm. The reason for the check on Δ being equal to zero is because if the discriminant of the quadratic is 0 then it only has one true root which is negative b divided by 2 times a. I was just hoping there was someone online that could confirm this algorithm for me, and if it has an error in it that they would point it out and explain what I did wrong.

public static float QuadraticFormula(float a, float b, float c) {
            float Δ = ToPower(b, 2) - (4 * a * c);
            return (Δ != 0) ? (-b + SquareRoot(Δ)) / (2 * a) : -(b / (2 * a));
        }

Thanks,
Jamie

I forgot to make sure that Δ was greater than zero because when the discriminant is a negative value we must use imaginary numbers to solve the quadratic and I didn't add handling for that at all. So just change line 3 to:

return (Δ != 0) && (Δ > 0) ? (-b + SquareRoot(Δ)) / (2 * a) : -(b / (2 * a));

You have the formula right, you have the results wrong. Remember, for every quadratic equations there are two roots (that's what the +- in the first line of the equation you posted. You need to either add or subtract the determinant).

Since it is possible that the results require the complex plane (this problem was one of the reasons that immaginary numbers were 'invented') you should take a look at the System.Numeric.Complex class. It will allow you to compute both roots, even if one is imaginary.

So how do I decide which complex number to use though? Or do I leave that up to the developers using the algorithm and just plug in the formula, since this is going into my application extension?

Formula for complex:

//-b      sqrt(-Δ)
//--- + i --------
//2a         2a

/* and */

//-b      sqrt(-Δ)
//--- - i --------
//2a         2a

You could override the ToString method, to print out the roots depending on whether the value of the discriminant is positive, zero or negative.

This is the new algorithm generated, but still am trying to figure out which complex numbers to use?

public static float[] QuadraticFormula(float a, float b, float c) {
            float Δ = ToPower(b, 2) - (4 * a * c);

            float[] r = new float[2];

            if (Δ > 0) {
                r[0] = (-b + SquareRoot(Δ)) / (2 * a);
                r[1] = (-b - SquareRoot(Δ)) / (2 * a);
            }

            else if (Δ < 0) { // i is never declared it's just a place holder.
                r[0] = -b / (2 * a) + i * (SquareRoot(-Δ)) / (2 * a);
                r[1] = -b / (2 * a) - i * (SquareRoot(-Δ)) / (2 * a);
            }

            else if (Δ == 0) {
                r[0] = -(b / (2 * a));
                r[1] = 0;
            }

            return r;

            //-b      sqrt(-Δ)
            //--- +- i--------
            //2a         2a

            //      -b +- sqrt(Δ)
            // x = ---------------
            //            2a
            //
            // Where Δ = b^2 - 4ac
        }

You could always use out parameters to return the 2 roots.

using System;
using System.Numerics;

namespace Testing {
    class Program {
        static void Main(string[] args) {
            int a = 3;
            int b = 4;
            int c = 2;

            Complex r1 = QuadPlus(a, b, c);
            Complex r2 = QuadMinus(a, b, c);

            if (r1.Imaginary == 0 && r2.Imaginary == 0) {
                Console.WriteLine("You have two real roots at {0:0.00} and {1:0.00}", r1.Real, r2.Real);
            } else if (r1.Imaginary == 0) {
                Console.WriteLine("You have one real root, {0:0.00}, and one imaginary root {1:0.00}", r1.Real, r2);
            } else if (r2.Imaginary == 0) {
                Console.WriteLine("You have one real root, {0:0.00}, and one imaginary root {1:0.00}", r2.Real, r1);
            } else {
                Console.WriteLine("You have two imaginary roots {0:0.00} and {1:0.00}", r1, r2);
            }

            Console.ReadLine();
        }

        static Complex QuadPlus(int a, int b, int c) {
            Complex ca = new Complex(a, 0);
            Complex cb = new Complex(b, 0);
            Complex cc = new Complex(c, 0);

            Complex root = (-cb + Complex.Sqrt(cb * cb - 4 * ca * cc)) / (2 * ca);

            return root;
        }

        static Complex QuadMinus(int a, int b, int c) {
            Complex ca = new Complex(a, 0);
            Complex cb = new Complex(b, 0);
            Complex cc = new Complex(c, 0);

            Complex root = (-cb - Complex.Sqrt(cb * cb - 4 * ca * cc)) / (2 * ca);

            return root;
        }

    }
}

So something of this nature for a single method returning two floats?

public static float[] QuadraticFormula(float a, float b, float c) {
	        Complex ca = new Complex(a, 0);
	        Complex cb = new Complex(b, 0);
	        Complex cc = new Complex(c, 0);

        	Complex Δ = (cb * cb) - (4 * ca * cc);
    	    Complex r1 = (-cb + Complex.Sqrt(Δ) / (2 * ca);
        	Complex r2 = (-cb - Complex.Sqrt(Δ) / (2 * ca);
	
    	    float[] r = new float[2];

            if (r1.Imaginary == 0 && r2.Imaginary == 0)
	            r = new float[]{Convert.ToSingle(r1.Real), Convert.ToSingle(r2.Real)};

            else if (r1.Imaginary == 0)
	            r = new float[]{Convert.ToSingle(r1.Real), Convert.ToSingle(r2.Imaginary)};

            else if (r2.Imaginary == 0)
	            r = new float[]{Convert.ToSingle(r1.Imaginary), Convert.ToSingle(r2.Real)};

            else
	            r = new float[]{Convert.ToSingle(r1.Imaginary), Convert.ToSingle(r2.Imaginary)};

	        return r;
        }

So something of this nature for a single method returning two floats?

public static float[] QuadraticFormula(float a, float b, float c) {
	        Complex ca = new Complex(a, 0);
	        Complex cb = new Complex(b, 0);
	        Complex cc = new Complex(c, 0);

        	Complex Δ = (cb * cb) - (4 * ca * cc);
    	    Complex r1 = (-cb + Complex.Sqrt(Δ) / (2 * ca);
        	Complex r2 = (-cb - Complex.Sqrt(Δ) / (2 * ca);
	
    	    float[] r = new float[2];

            if (r1.Imaginary == 0 && r2.Imaginary == 0)
	            r = new float[]{Convert.ToSingle(r1.Real), Convert.ToSingle(r2.Real)};

            else if (r1.Imaginary == 0)
	            r = new float[]{Convert.ToSingle(r1.Real), Convert.ToSingle(r2.Imaginary)};

            else if (r2.Imaginary == 0)
	            r = new float[]{Convert.ToSingle(r1.Imaginary), Convert.ToSingle(r2.Real)};

            else
	            r = new float[]{Convert.ToSingle(r1.Imaginary), Convert.ToSingle(r2.Imaginary)};

	        return r;
        }

If you are going to return an array of floats, shouldn't it have 4 values in it? There should be 1 for the real part of the first root, 1 for that imaginary part, and the same for the second root. Therefore your second 'else if' should be changed into 'if' and r2 should get put into index 2 for the real and 3 for the imaginary.

Instead of using floats I would just use an array of 2 Complex datatypes as Momerath pointed out since it would be easier for a developer to understand the return value (although it's totally up to you how you want your own code to work :P ).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.