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