I have a code for a quadratic formula solver, but I can't get NaN to behave with my if
statements.
package homework;
import java.util.Scanner;
import java.lang.Math;
// My Name 9/18/18
// The purpose of this class is to allow a user to input an a,b, and c value and perform the quadratic equation on them
public class QuadFormHW
{
public static void main(String[] args)
{
double a,b,c,answer1,answer2;
Scanner inputReader = new Scanner(System.in);
System.out.print("Please enter an \"a\" \"b\" and \"c\" value.");
a = inputReader.nextDouble();
b = inputReader.nextDouble();
c = inputReader.nextDouble();
answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
if (answer1 == NaN)
{
System.out.print("Error cannot calculate");
}
else if (answer2 == NaN)
{
System.out.print("Error cannot calculate");
}
else
{
System.out.printf("Your answers are: %.3f , %.3f",answer1,answer2);
}
inputReader.close();
}
}
Can anyone help me understand why NaN
isn't a acceptable value?