Your problem is not a complete understanding of some elementary school mathematics. Your more "advanced problem" is not understanding
approximate methods of numerical calculations, which is a much more complicated issue. The quadratic equation problem solved numerically is approximate by its nature.
I can assume that you are analyzing/solving the equation
ax² + bx + c = 0
You are doing it wrong.
First of all, for
a ≡ b ≡ c ≡ 0
, "Roots cannot be determined" it a totally wrong answer. Correct answer will be "Any real number is a root". Also this is true: "Any complex number is a root". See the difference? You can easily check up this statement.
Another cases are when there are no real roots (is would be much nicer to output complex root) and exactly two roots. You really should not make a distinction between having two roots and only one root. Conceptually, you always have two or zero roots, and the two roots have the same value. The set of cases when the two roots are equal is of the
measure null. More accurately, two different roots is the case of
general position. For some basics of it, please see:
https://en.wikipedia.org/wiki/General_position[
^].
It has a special meaning for approximate calculations (remember, those calculations are always approximate; not only the roots, but even
a
,
b
and
c
are, by definition, know with limited accuracy. Look at the quadratic function graph. As it get closer to the position where the
extremum of the function value touches the x axis, two roots get close and close. With approximate calculations, you don't know exactly when it touches. The roots just became "equal" when the difference between them becomes lower than the accuracy of the
real number representation (floating-point, in our case). In fact, you can slightly modify one of the coefficient while having the roots "equal" in this sense, which, of course, is not the case for
ideal real numbers. So, the solution will only be correct if you calculate both roots, "equal" or not.
Now, your formula for calculation of the roots is correct, but it can be considered as questionable from the point of view of best accuracy of approximate calculation. As I remember, in his
The Art of Computer Programming, Donald Knuth insisted that one should choose one of the roots of your solution, the one causing less
loss of precision due to + or − sign in the solution formula; and the second root should be found using the Viera's formula:
https://en.wikipedia.org/wiki/Quadratic_equation[
^],
https://en.wikipedia.org/wiki/Vieta%27s_formulas[
^].
—SA