为什么输出结果是 (Nan,Nan)?

3

我是Java的新手,现在需要完成一项任务,但是卡住了。这个任务是使用二次方程式找到两条线的交点。老师要求我为类设置特定的输入,因此d = 5,f = -3,g = 2,m = 1和b = 3,两个交点应该是(1,4)和(-0.20,2.8)。问题是,输出返回的是(NaN,NaN)和(NaN,NaN),而不是正确的答案。我的代码有问题吗?

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 + ")");
}
}

2
我很困惑。输出是(4.42,7.42)和(3.57,.57),还是输出为(Nan,Nan) - Kevin
哦等等,输出为(Nan, Nan)。 - Gabrielle Ringor
3
在Java中,“^”不是指数运算符。z^2并不是你想象中的指数运算。 - azurefrog
1
提示:(z^2 - 4 * a * c)的值是多少? - dsh
@azurefrog:但对于平方,最简单的解决方案是 z*z - Lutz Lehmann
显示剩余4条评论
3个回答

4

^ 是Java中的异或运算符,而不是指数运算符。因此,表达式z ^ 2 - 4 * a * c会计算出一个负数。

根据您提供的输入,z = -4,a = 5,c = -1。该表达式转化为-4 ^ 2 - 4 * 5 * -1。请注意,*+优先级高于^,即计算顺序是(-4 ^ (2 - ((4 * 5) * -1))) = -22

然后,您试图找到-22的平方根,根据Math.sqrt(),它是NaN

使用Math.pow(z, 2),或者直接使用z * z

Math.sqrt(z * z - 4 * a * c);  // Note: This time operator precedence works,
                               // But you should use parentheses wherever
                               // the expression seems ambiguous.

1
首先,^ 不是幂运算符,导致NaN的原因是将负参数传递给Math.sqrt函数。
来自Java参考文档(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.

0

你的操作顺序导致了NaN结果。 尝试这样做(为方便添加变量):

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接