Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Solving the Quadratic Equation

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Solving the Quadratic Equation

    I'm a complete beginner so I signed up for an edx.com MOOC. I don't think the course is very active anymore so I figured I'd join up here to ask questions when needed. I'm in the process of solving the Quadratic Equation, and I'm not getting the correct results.

    the IO is an imported file from the class instructor
    Using the numbers a = 5, b = 6, and c = 1
    I should get the output of -0.2 and -1.0
    I get -0.2 and 0.14833147735478827

    Any help?

            double a;
            double b;
            double c;
            double d;
            double first_x;
            double second_x;
     
            IO.output("Enter a: ");
            a = IO.inputDouble( );
            IO.output("Enter b: ");
            b = IO.inputDouble( );
            IO.output("Enter c: ");
            c = IO.inputDouble( );
     
            d = Math.pow(b,2);
            first_x = (-b + Math.sqrt(d - 4 * a * c)) / (2 * a);
            second_x = (-b + Math.sqrt(d + 4 * a * c)) / (2 * a);
     
            IO.outputln("First Solution for x = " + first_x);
            IO.outputln("Second Solution for x = " + second_x);

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Solving the Quadratic Equation

    Nothing wrong with your code. It's your math. Your quadratic formula is incorrect. And when you look it up on Wiki, you should check out the computational formula used in programming. It helps avoid overflow issues during intermediate calculations.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Sep 2018
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Solving the Quadratic Equation

    Quote Originally Posted by jim829 View Post
    Nothing wrong with your code. It's your math. Your quadratic formula is incorrect. And when you look it up on Wiki, you should check out the computational formula used in programming. It helps avoid overflow issues during intermediate calculations.

    Regards,
    Jim
    Got it, thanks. I made a post saying I still don't see the error, the BOOM it hit me.

    Could you still elaborate on the computational formula you mentioned? Didn't find anything referring to that in my search.

    Thanks
    Attached Images Attached Images

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Solving the Quadratic Equation

    Mea Culpa! I thought it was mentioned in the Wiki. Here is an article on it.

    http://people.csail.mit.edu/bkph/art...Quadratics.pdf

    Here is a contrived example. Notice that a and b are near their maximum value for a 32 bit integer. So if you add them you will get overflow and in this case, a negative number. Thus the "average" will be a negative number and incorrect. So the trick is to divide first and add the quotients. Computing an average this way is more expensive (i.e. time consuming) since dividing is an expensive operation compared to addition. But in some cases it is required. You can run into similar problems using double values. If you're interesting in understanding how positive and negative integers are stored you can read this -> Two's complement

    public class OverFlowDemo {
       public static void main(String[] args) {
     
    	  int a = 2_128_928_234;
    	  int b = 2_023_192_252;
     
    	  System.out.println(a+b);  //overflow - the sum won't fit in an int.  
    	  System.out.println((a + b)/2);  // normal averaging - wrong answer
     
    	  System.out.println();
    	  System.out.println(a/2);  // so divide first, then add
    	  System.out.println(b/2);
     
    	  System.out.println(a/2 + b/2);  //alternate way - correct answer
       }
    }

    Note: The above is not really a Java issue but is germane to computer science and many programming languages and computer architectures.

    Regards,
    Jim
    Last edited by jim829; September 29th, 2018 at 09:23 AM. Reason: Additional info

  5. The Following User Says Thank You to jim829 For This Useful Post:

    airac13 (September 29th, 2018)

Similar Threads

  1. A couple of issues with my quadratic equation solver.
    By EatMyBible in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 10th, 2013, 07:52 PM
  2. Solving linear equation systems including sparse matrices
    By GregXel in forum Java Theory & Questions
    Replies: 1
    Last Post: November 24th, 2012, 12:05 PM
  3. quadratic equation solver help!
    By overlord in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 20th, 2011, 11:39 AM
  4. Help with Quadratic forumla equation in java please.
    By taylor6132 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2010, 07:27 PM
  5. help!! quadratic program
    By dscrudato21xo in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 18th, 2009, 05:13 PM