if b+sqrt(b^2-4*a*c)>b-sqrt(b^2-4*a*c)
if b+sqrt(b^2-4*a*c)<b-sqrt(b^2-4*a*c)
That code will not assign d under a few circumstances:
- b+sqrt(b^2-4*a*c) == b-sqrt(b^2-4*a*c) -- which would occur if sqrt(b^2-4*a*c) == 0
- one of the values is nan
- sqrt(b^2-4*a*c) is imaginary but b is real; in that case, the > operator would compare only the real parts, and the real parts would be equal
Note:
b+sqrt(b^2-4*a*c)<b-sqrt(b^2-4*a*c) implies b + sqrt(b^2-4*a*c) - b < b - sqrt(b^2-4*a*c) - b implies sqrt(b^2-4*a*c) < - sqrt(b^2-4*a*c) implies 2*sqrt(b^2-4*a*c) < 0 implies sqrt(b^2-4*a*c) < 0 . However, algebraically, sqrt() is defined as "principle square root", which is positive when the value is positive, and is 1i * sqrt(-(b^2-4*a*c)) when b^2-4*a*c < 0 -- so sqrt(b^2-4*a*c) < 0 cannot happen unless you define an ordering of imaginary values with respect to 0. The MATLAB < operator ignores the imaginary component... but then you would be comparing 0 to 0, which would not be < .
Which is to say that your second test can never succeed, and your first test is always true unless b^2-4*a*c == 0 (a ligitimate test) or b^2-4*a*c < 0 (because the imaginary component is ignored)
Best Answer