为啥是输出 (Nan,Nan)? [复制]

Posted

技术标签:

【中文标题】为啥是输出 (Nan,Nan)? [复制]【英文标题】:Why is the output (Nan,Nan)? [duplicate]为什么是输出 (Nan,Nan)? [复制] 【发布时间】:2015-12-28 14:52:06 【问题描述】:

所以我是 Java 新手,我有一项作业要做,但我被困住了。该类应该使用二次方程找到两条线的交点。我被告知有特定的类输入,所以 d = 5,f = -3,g = 2,m = 1 和 b = 3,两个交点应该是 (1,4) 和 (-.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 + ")");


【问题讨论】:

我很困惑。是输出(4.42,7.42) and (3.57,.57),还是输出(Nan,Nan) 哦等等,输出是(Nan, Nan) ^ 不是 Java 中的指数运算符。 z^2 不是你想的那样。 提示:(z^2 - 4 * a * c) 的值是多少? 见Java operators。对于求幂,请使用Math.pow() 【参考方案1】:

^ is the xor operator in java and not the exponentiation operator。因此,表达式z ^ 2 - 4 * a * c 计算为负数。

根据您提供的输入,z = -4, a = 5, c = -1。该表达式转换为-4 ^ 2 - 4 * 5 * -1。注意*+ 有一个higher precedence than ^,即求值顺序是(-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.

【讨论】:

【参考方案2】:

首先 ^ 不是指数运算符,导致 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.

【讨论】:

【参考方案3】:

是您的操作顺序导致您获得 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;

【讨论】:

以上是关于为啥是输出 (Nan,Nan)? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

为啥 NaN === NaN 是假的? [复制]

为啥 NaN 大于 JavaScript 中的任何数字? [复制]

为啥我在 GAN 的训练鉴别器和生成器中得到 nan 损失值? [复制]

熊猫不识别 np.nan 值吗? [复制]

js 为啥计算结果老是出现NaN

jQuery 脚本在文本框中显示 NaN,如何删除 NaN? [复制]