Hello all,
I am trying to write a function in C++ that solves for X using the quadratic equation. This is what I have written initially, which seems to work as long as there are no complex numbers for an answer:
float solution1 = (float)(-1.0 * b) + (sqrt((b * b) - (4 * a * c)));
solution1 = solution1 / (2*a);
cout << "Solution 1: " << solution1 << endl;
float solution2 = (float)(-b) - (sqrt((b*b) - (4 * a * c)));
solution2 = solution2 / (2*a);
cout << "Solution 2: " << solution2;
If, for example, I use the equation: x^2 - x - 6, I get the solution 3, -2 correctly.
My question is how would I account for complex numbers....for example, given the equation:
x^2 + 2x + 5
Solving by hand, I would get -1 + 2i, -1 - 2i.
Well, I guess two question, can I write the above better and also make it account for the complex number?
Thanks for any help!