android中Math.sqrt()的奇怪输出
所以在我的应用程序中有一部分,它使用二次公式来解和。但我得到一个奇怪的输出。这是我得到的答案,而不是实际的答案,“NaN或NaN” 此代码在单击按钮时执行android中Math.sqrt()的奇怪输出,android,Android,所以在我的应用程序中有一部分,它使用二次公式来解和。但我得到一个奇怪的输出。这是我得到的答案,而不是实际的答案,“NaN或NaN” 此代码在单击按钮时执行 h1 = Double.parseDouble(a); h2 = Double.parseDouble(b); h3 = Double.parseDouble(c); sq2 = (h1 * h1 - 4* 0.5 *h3*h1); sq = (h1 * h1 - 4* 0.
h1 = Double.parseDouble(a);
h2 = Double.parseDouble(b);
h3 = Double.parseDouble(c);
sq2 = (h1 * h1 - 4* 0.5 *h3*h1);
sq = (h1 * h1 - 4* 0.5 *h3*h1);
fin = h1 + (Math.sqrt(sq2))/(2* 0.5 * h3);
fin2 = (h1 - (Math.sqrt(sq)))/(2* 0.5 * h3);
AlertDialog.Builder ans4 = new AlertDialog.Builder(Eq2.this);
ans4.setTitle("ANSWER");
ans4.setMessage(fin + " or " + fin2);
ans4.setPositiveButton("OKAY!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
e4.setText(fin + "or" + fin2);
}
});
ans4.show();
当您对负数执行
Math.sqrt
时,会得到NaN
(不是数字),请检查您的数学
在Math.sqrt
之前使用Math.abs
应该会得到一个数字(它使您对Math.sqrt
的输入为正),但可能不是正确的数字
fin = h1 + (Math.sqrt(Math.abs(sq2))/(2* 0.5 * h3);
fin2 = (h1 - (Math.sqrt(Math.abs(sq))))/(2* 0.5 * h3);
当您对负数执行
Math.sqrt
时,会得到NaN
(不是数字),请检查您的数学
在Math.sqrt
之前使用Math.abs
应该会得到一个数字(它使您对Math.sqrt
的输入为正),但可能不是正确的数字
fin = h1 + (Math.sqrt(Math.abs(sq2))/(2* 0.5 * h3);
fin2 = (h1 - (Math.sqrt(Math.abs(sq))))/(2* 0.5 * h3);
NaN
:“不是数字”。换言之,它大便了
代码中的原因几乎肯定是取负值的平方根(Math.sqrt()
)
如果你正在尝试,那么
NaN
:“不是数字”。换言之,它大便了
代码中的原因几乎肯定是取负值的平方根(Math.sqrt()
)
如果你正在尝试,那么
检查
h1>=2*h3
。如果不是,你会得到一个负数。(到底什么是h2
)检查h1>=2*h3
。如果不是,你会得到一个负数。(到底什么是h2
)