# How to handle quadratic roots that don't exist?

• October 27th, 2009, 03:19 PM
kairojya
How to handle quadratic roots that don't exist?
Hi guys, I'm a new Computer Science major and taking a Java class this semester. I'm unsure about 1 part of an assignment I've been working on. I need to display a few strings in the event that the roots to a quadratic equation do not exist. I'm wondering how to go about doing this.

From what a know if a value doesn't exist the program would return a value of "NaN", is that correct? However in the NetBeans IDE I'm not able to set and if statement such as:

Code :

```if(n > 0){   double discriminant; double root1; double root2;   discriminant = Math.sqrt(b*b-4*a*c); root1 = (-b + discriminant)/(2*a); root2 = (-b - discriminant)/(2*a);   PrintWriter out = new PrintWriter(new FileWriter("results.dat", true));   out.println("Root1: " + root1 + ", Root2: " + root2); out.close();   if(root1 = NaN, root2 = NaN) { System.out.println("Quadratic equation with the following coefficients:"); System.out.println("a: " + a + ";" + " b: " + b + ";" + " c: " + c + ";"); System.out.println("has the following roots: " + root1 + " and " + root2); } }```

As you can see, what I'm wondering how to accomplish is the nested If statement down at the bottom. Int "n" is a number entered into the command line when running the App. Variables a, b, c are doubles asked from the user.

One other part I'm confused on is how to specify one parameter or the other in the IF statement, as I wrote:

if(root1 = NaN, root2 = NaN)

Thanks for the help!

EDIT: The strings we're copied from a previous assignment and we'll be changed later to display different information, such as "The quadratic equation has non-real roots." or something along those lines. Sorry for the confusion.
• October 28th, 2009, 12:21 PM
kairojya
Re: How to handle quadratic roots that don't exist?
Got some help with this issue elsewhere, just going to post my findings in case anyone brings this thread up in a search:

Code :

```if(n > 0){   double discriminant; double root1; double root2;   discriminant = Math.sqrt(b*b-4*a*c); root1 = (-b + discriminant)/(2*a); root2 = (-b - discriminant)/(2*a);   PrintWriter out = new PrintWriter(new FileWriter("results.dat", true));   out.println("Root1: " + root1 + ", Root2: " + root2); out.close(); // Closing a file after writing to it.   boolean b1 = Double.isNaN(root1); boolean b2 = Double.isNaN(root2);   if(b1 || b2) { System.out.println("Quadratic equation with the following coefficients:"); System.out.println("a: " + a + ";" + " b: " + b + ";" + " c: " + c + ";"); System.out.println("has roots that don't exist."); } }```