0

So I'm trying to code this equation using java:

enter image description here

I'm taking a, b, and c from the user. This is the code I have so far:

import java.util.Scanner;
class QaudraticFunction{
    public static void main(String []args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a: ");
        double a = input.nextDouble();

        System.out.println("Enter b: ");
        double b = input.nextDouble();

        System.out.println("Enter c: ");
        double c = input.nextDouble();

        double val1 = (Math.pow(b,2.0)) - (4.0*a*c);
        double discriminant = Math.sqrt(val1);
        double val2 = (-b)-(discriminant);
        double r2 = val2/(2.0*a);

        System.out.println("r2 = " + r2);
    }
}

I think my issue is a logical error because the program compiles and runs correctly. When I enter the values for a, b and c. I get r2 = NaN

Vishal
  • 110
  • 2
  • 11
  • `When I enter the values for a, b and c` What values do you enter? – copeg Sep 21 '16 at 22:38
  • 2
    Your code looks fine. You're probably providing coefficients for a parabola which does not intersect the x-axis. I forget the exact math terminology, but try some other numbers – Kon Sep 21 '16 at 22:38
  • 1
    you'll get NaN when you do something not allowed like dividing by 0 or taking the square root of a negative number which are both possibilities based on your formula. It all comes down to your numbers being used – RAZ_Muh_Taz Sep 21 '16 at 22:39
  • Code seems ok, but `(Math.pow(b,2.0))` is better written as `b*b` *(better performance and simpler)*, and `(4.0*a*c)` can be simply `4*a*c`. You don't need all those parenthesis, since Java knows standard [operator precedence](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) rules. – Andreas Sep 21 '16 at 22:41
  • @Kon I mostly entered small values such as 1, 2, 3 or 5, 10, 15 – Vishal Sep 21 '16 at 22:43
  • @Vishal OK, but do you understand what this equation does? – Kon Sep 21 '16 at 22:44
  • 1
    With input 1,2,3 `b*b-4*a*c` is `2*2-4*1*3 = 4 -12 = -8`. Trying to take square root of a negative number gives the error (see all other comments). You have the same problem with 5,10,15, `10*10-4*15*15 = 100 - 900 = -800`. Try with 1, 3, 2, you should get -1 and -2. (You can also do the computations by hand on a piece of paper to get a better understanding.) – Sci Prog Sep 22 '16 at 01:44

1 Answers1

3

Two possible reasons you are seeing a NaN.

The denominator is 0. This is only possible if a is set to 0 or 0.0. I'm going to assume that's not the case.

The other possibility is that you are performing a square root of a negative number, which (in java) is a NaN. See Math.sqrt javadoc for more details.

If the argument is NaN or less than zero, then the result is NaN.

blr
  • 908
  • 4
  • 8
  • Oh I understand now. So what you're saying is that the value of 'b' should be higher than 'a' and 'c' otherwise the square root will be negative. – Vishal Sep 21 '16 at 22:49
  • 1
    Sort of. I'm speculating here since you haven't given the value of a,b and c but that will be my best guess. If you need recommendations on how to deal with it, one cheap way will be to just check if val1 is NaN or b^2 > 4*a*c; if not throw an error. Other one is to use [Apache Common's Complex](https://commons.apache.org/proper/commons-math/userguide/complex.html) data type. – blr Sep 21 '16 at 23:17