3

So I'm new to Java and I have an assignment to do for class, but I'm stuck. The class is supposed to find the intersection of two lines using the quadratic equation. I was told to have specific inputs for class, so d = 5, f = -3, g = 2, m = 1 and b = 3 and the two intersections are supposed to be (1,4) and (-.20,2.8). The problem I'm running into is that the output returns (NaN,NaN) and (NaN,NaN) instead of the right answer. Is there anything wrong with my code that is making me get this answer?

public class Intersect{
public static void main(String args[]){

    //prompt user for parabola vars d f g
    System.out.println("Enter the constant d:");
    int d = IO.readInt();
    System.out.println("Enter the constant f:");
    int f = IO.readInt();
    System.out.println("Enter the constant g:");
    int g = IO.readInt();

    // y = dx^2 + fx + g

    //promt user for line vars m b 
    System.out.println("Enter the constant m:");
    int m = IO.readInt();
    System.out.println("Enter the constant b:");
    int b = IO.readInt();

    // y = mx + b

    //compute intersection
    // dx^2 + fx + g = mx + b 
    // x^2 * (d) + x * (f-m) + (g-b) = 0 

    int a = d;
    int z = (f-m);
    int c = (g-b);

    double x1 = -z + (Math.sqrt (z^2 - 4 * a * c) / (2 * a));
    double x2 = -z - (Math.sqrt (z^2 - 4 * a * c) / (2 * a));
    double y1 = m * x1 + b;
    double y2 = m * x2 - b;

    //output each intersection on its own line, System.out.println() is ok for this answer submission
    System.out.println("The intersection(s) are:");
    System.out.println("(" + x1 + "," + y1 + ")");
    System.out.println("(" + x2 + "," + y2 + ")");
}
}

3 Answers3

4

^ is the xor operator in java and not the exponentiation operator. Therefore, the expresson z ^ 2 - 4 * a * c computes to something negative.

From the input you provide, z = -4, a = 5, c = -1. The expression translates to -4 ^ 2 - 4 * 5 * -1. Note that * and + have a higher precedence than ^, i.e. the order of evaluation is (-4 ^ (2 - ((4 * 5) * -1))) = -22.

Then you're trying to find the square root of -22, which, according to Math.sqrt(), is NaN.

Use Math.pow(z, 2), or simply use z * z instead:

Math.sqrt(z * z - 4 * a * c);  // Note: This time operator precedence works,
                               // But you should use parentheses wherever
                               // the expression seems ambiguous.
Community
  • 1
  • 1
John Bupit
  • 10,406
  • 8
  • 39
  • 75
1

First of all ^ is not an exponentiation operator, what causes Nan is the fact that you pass in a negative argument to Math.sqrt.

From java reference ( http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html ):

public static double sqrt(double a)
Returns the correctly rounded positive square root of a double value.     Special cases:
If the argument is NaN or less than zero, then the result is NaN.
If the argument is positive infinity, then the result is positive infinity.
If the argument is positive zero or negative zero, then the result is the same as the argument.
Otherwise, the result is the double value closest to the true mathematical square root of the argument value.
Parameters:
a - a value.
Returns:
the positive square root of a. If the argument is NaN or less than zero, the result is NaN.
Pavel
  • 1
  • 3
  • 17
  • 51
0

Its your order of operations that is causing you to get NaN results. Try this(added variables for convenience):

int a = d;
int z = f - m;
int negZ = -z;
int c = g - b;

double sq = Math.sqrt((z * z) - (4 * a * c));
double a2 = 2 * a;
double x1 = (negZ + sq) / a2;
double x2 = (negZ - sq) / a2;
double y1 = (m * x1) + b;
double y2 = (m * x2) - b;
MGM_Squared
  • 167
  • 7