Note: I didn't compile your program so I don't know what your program does for this condition
I've came across many algebraic equations where the roots are real as well as same
e.g a=1 b=2 c=1 so sqrt(pow(b,2)-4*a*c) will become 0 so x1 and as well as x2 will get the same value and I see your code has no implementation to solve that kind of equation.
1) It doesn't matter what the real world quadratic equations say, if I run your program, I have the freedom to (stupidly or not), put a zero where asked for "a". The program should recognize this and ask for a different number, not create a divide by zero exception. One great axiom of programming is to ALWAYS ASSUME your input data is dirty, incorrect, and malicious. You need to protect your program from the user.
I do not wish to be rude. If I am, I'm really sorry.
1) In a quadratic equation, 'a' can never be 0. So, where can a division by zero problem occur?
2) Taking square root twice is not a big issue.. It is computed in a matter of seconds.. I do agree it is inefficient in the extreme case..
3) An alternative can have an improved feature.
This is better than the others as you avoid the domain error in sqrt(). A couple of things:
- pow(b, 2) is better written as b*b
- it's probably better not to evaluate sqrt(d) twice
- the original returned two floats, you should return an error instead of complex numbers
Last Visit: 31-Dec-99 18:00 Last Update: 10-Aug-22 20:40