Trying to get printf to work

I made a program to calculate the roots of quadratic equation by the user entering the A, B, and C values. Here is what I have so far

package quadratic;

import java.util.Scanner;

publicclass Quadratic{

/**

* @param args

*/

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

double A, B, C;

Scanner input =new Scanner (System.in);

System.out.println("Enter A, then B, then C");

A = input.nextDouble();

B = input.nextDouble();

C = input.nextDouble();

System.out.printf("Root one is %g", negBplus(A, B, C));

}

publicstaticdouble negBplus(double a,double b,double c){

return ((-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / 2*a);

}

publicstaticdouble negBminus(double a,double b,double c){

return ((-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / 2*a);

}

}

When I try to run the program, I get "Root one is NaN." And thats when I use the %g sign. I have used other letters like %f, %d, and %e, but none seem to work. I have tried to make the entire program run on ints but it says that Math.sqrt, and Math.pow are both equations that produce doubles. How can I get it so that it properly displays the roots?

[2360 byte] By [secretempire1a] at [2007-11-27 8:45:50]
# 1
Are you aware of what "NaN" means?~
yawmarka at 2007-7-12 20:47:13 > top of Java-index,Java Essentials,New To Java...
# 2
not a number, but idk why
secretempire1a at 2007-7-12 20:47:13 > top of Java-index,Java Essentials,New To Java...
# 3
Not for all A, B and C in R exist a real solution. What happens when:Math.pow(b, 2) < 4*a*c?Try it with: -1 -2 3.
prometheuzza at 2007-7-12 20:47:13 > top of Java-index,Java Essentials,New To Java...
# 4
I getEnter A, then B, then C-1-23Root one is -3.00000So how would I modify my code to work with all numbers, and display "i" if the number is not real?
secretempire1a at 2007-7-12 20:47:13 > top of Java-index,Java Essentials,New To Java...
# 5

> ...

> So how would I modify my code to work with all

> numbers, and display "i" if the number is not real?

Check if (b*b)-(4*a*c) < 0. If it is, just finish the rest of the calculation with the absolute value of (b*b)-(4*a*c) and append an i at the end.

prometheuzza at 2007-7-12 20:47:13 > top of Java-index,Java Essentials,New To Java...
# 6

public static double negBplus(double a, double b, double c){

if((b*b)-(4*a*c) < 0){

return Math.abs((b*b)-(4*a*c));

}

else return ((-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / 2*a);

}

public static double negBminus(double a, double b, double c){

if((b*b)-(4*a*c) < 0){

return Math.abs((b*b)-(4*a*c));

}

return ((-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / 2*a);

}

}

woudl that work? and the roots wont always have an i, so how would i add one only when its not a num?

secretempire1a at 2007-7-12 20:47:13 > top of Java-index,Java Essentials,New To Java...
# 7

> ...

> woudl that work? and the roots wont always have an

> i, so how would i add one only when its not a num?

No, for one: you cannot return a double. You must return a String because when a solution in the complex plane is found, you cannot represent it as a real number. This is what I had in mind:

public static String solve(double a, double b, double c) {

if(b*b < 4*a*c) {

return (-b/(2*a))+" +/- "+(Math.sqrt(Math.abs(b*b - 4*a*c))/(2*a))+"*i";

} else {

return "normal solution(s) as a String...";

}

}

Not tested! Adjust as you see fit.

Good luck.

prometheuzza at 2007-7-12 20:47:13 > top of Java-index,Java Essentials,New To Java...