0

I'm reading a Introductory programming book by Robert Sedgewick and Kevin Wayne.

In one of the examples they implement a quadratic class as follows:

public class Quadratic
{
    public static void main(String[] args)
    {
        double b = Double.parseDouble(args[0]);
        double c = Double.parseDouble(args[1]);
        double discriminant = b * b - 4.0 * c;
        double d = Math.sqrt(discriminant);
        System.out.println((-b + d) / 2.0);
        System.out.println((-b - d) / 2.0);
    }
}

The author omits the 'a' coefficient of the quadratic formula. Is this because the 'a' coefficient can be cancelled out (numerator / denominator)?

Based on the feedback… Would the following be the correct solution:

public static void main(String[] args)
    {
        double b = Double.parseDouble(args[0]);
        double c = Double.parseDouble(args[1]);
        double a = Double.parseDouble(args[2]);
        double discriminant = b * b - 4.0 * a * c;
        double d = Math.sqrt(discriminant);
        System.out.println((-b + d) / (2.0 * a));
        System.out.println((-b - d) / (2.0 * a));
    }
dcrearer
  • 1,972
  • 4
  • 24
  • 48
  • Not if the discriminant is negative, or a == 0. This is a naive implementation that doesn't take special cases into account (a == 0, b == 0, c == 0). – duffymo Mar 08 '16 at 20:58
  • @duffymo thanks for the feedback can you expound on your thought? Do you have an alternate solution? – dcrearer Mar 08 '16 at 21:13
  • Yes, go through each of the three cases I mentioned and see how that changes the solution. For example, if b = 0 there are two solutions: +/- sqrt(c/a). if a = 0 there's only one solution: -c/b. If c = 0 there are two solutions: 0 and -b/a. If the discriminant is negative, the two solutions are complex numbers, conjugates of each other. You should account for all of these. – duffymo Mar 08 '16 at 21:47

2 Answers2

4

No, the author might have implemented that algorithm differently. Assuming the general case, a can't get cancelled as -b factor doesn't consist of a.

Formula for finding the roots of a quadratic equation is :-

roots = (-b +(-) sqrt((b^2) - (4*a*c))) / (2*a).
     // here - alongwith + represents how to find second root.

I'd suggest you to go through the commonly used way. If the author has used different conventions, then please don't follow that.

Please follow the standard/common way. It is easy to understand.

Based on the feedback… Would the following be the correct solution:..

The solution which you have added to the question as an edit appears correct. So, I'd suggest you to go that way.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • can you have a look at my edit and provide some thoughts. – dcrearer Mar 08 '16 at 17:43
  • This is incorrect. The author has cancelled it out of the equation, not out of the formula for quadratic roots. Could you please revise your answer as it seems to be the accepted one ? – user1952500 Mar 08 '16 at 18:10
  • @user1952500 - SORRY, it is the correct/standard formula. It can't be wrong. Next, I don't have the book. How can I say what has author cancelled? Let OP go with the standard formula approach. Thanks for your point though. – Am_I_Helpful Mar 08 '16 at 18:12
  • 1. If you don't have the book, don't mention that the author is wrong. Say that what is mentioned in the problem is not the standard formula. 2. In mathematics, there are no fixed formulae and there are hundreds of ways of implementing the same thing. – user1952500 Mar 08 '16 at 18:15
  • @user1952500 - 1. I've edited the post. 2. I disagree. There are things like complexity in understanding things, detailed-length of the proof,etc. I don't want to get into this. But, THANKS again for your genuine concern. – Am_I_Helpful Mar 08 '16 at 18:19
  • 1
    The reason I was concerned about the wording is: Sedgewick (the author) is a well-known Computer Scientist, and is the guy who makes the conventions that you speak of. You can read more about him on wiki. It's not nice to call him wrong unless you are very sure about the context. – user1952500 Mar 08 '16 at 18:25
  • @user1952500 - I know pretty much about him. It is accidentally that I wrote those harsh words for him. I deeply express my sincere apology to him. :D – Am_I_Helpful Mar 08 '16 at 18:27
3

I think the authors are assuming a certain normalization wherein the leading coefficient of the quadratic equation is 1.

So, for instance:

2x2 + 4x + 8 = 0

would be represented as

x2 + 2x + 4 = 0

These are both the same equation, it's just that one has been normalized, so to speak.

arshajii
  • 127,459
  • 24
  • 238
  • 287