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

1. ## 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);```  Reply With Quote

2. ## 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  Reply With Quote

3. ## Re: Solving the Quadratic Equation Originally Posted by jim829 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  Reply With Quote

4. ## Re: Solving the Quadratic Equation

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

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  Reply With Quote

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

airac13 (September 29th, 2018)