3

I am working on a function for the quadratic formula in java eclipse mars, and when I compile the code it outputs NaN when mathematically this answer is possible and i should get 2.0 please help

import java.util.Scanner;
public class Quadradic1 {
    public static void main(String[] args) {    
        double a;
        double b;
        double c;
        double x;
        System.out.print("Input A B C: ");
        Scanner input = new Scanner(System.in);
        a = input.nextDouble();
        b = input.nextDouble();
        c = input.nextDouble();
        x = (-b + Math.sqrt(b * b + 4 * a * c))/(2 * a);
        System.out.println("Quadratic1 " + x);
    }
}

Sorry the values I entered are a=1 b=2 and c=-8

  • How are we supposed to help you if you don't even tell us which values you are inputing to the function? According to the informations you gave us you could directly input `NaN` from stdin, or at least we can't exclude it. – Jack Sep 17 '15 at 02:58
  • I'm getting "Quadratic1 2.0" – Yassin Hajaj Sep 17 '15 at 02:58

1 Answers1

0

For your inputs b * b + 4 * a * c evaluates to -28. There is not such thing as the square root of a negative number

logee
  • 5,017
  • 1
  • 26
  • 34