Java 如何测试双精度值是否等于 NaN?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1456566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you test to see if a double is equal to NaN?
提问by Eric Wilson
I have a double in Java and I want to check if it is NaN
.
What is the best way to do this?
我在 Java 中有一个 double,我想检查它是否是NaN
. 做这个的最好方式是什么?
采纳答案by Ben S
Use the static Double.isNaN(double)
method, or your Double
's .isNaN()
method.
使用静态Double.isNaN(double)
方法或您Double
的.isNaN()
方法。
// 1. static method
if (Double.isNaN(doubleValue)) {
...
}
// 2. object's method
if (doubleObject.isNaN()) {
...
}
Simply doing:
简单地做:
if (var == Double.NaN) {
...
}
is not sufficientdue to how the IEEE standard for NaN and floating point numbersis defined.
回答by Andrew Hare
Try Double.isNaN()
:
Returns true if this Double value is a Not-a-Number (NaN), false otherwise.
如果此 Double 值是非数字 (NaN),则返回 true,否则返回 false。
Note that [double.isNaN()
] will not work, because unboxed doubles do not have methods associated with them.
请注意, [ double.isNaN()
] 将不起作用,因为未装箱的双打没有与之关联的方法。
回答by p.g.gajendra babu
Beginners needs practical examples. so try the following code.
初学者需要实际的例子。所以试试下面的代码。
public class Not_a_Number {
public static void main(String[] args) {
// TODO Auto-generated method stub
String message = "0.0/0.0 is NaN.\nsimilarly Math.sqrt(-1) is NaN.";
String dottedLine = "------------------------------------------------";
Double numerator = -2.0;
Double denominator = -2.0;
while (denominator <= 1) {
Double x = numerator/denominator;
Double y = new Double (x);
boolean z = y.isNaN();
System.out.println("y = " + y);
System.out.println("z = " + z);
if (z == true){
System.out.println(message);
}
else {
System.out.println("Hi, everyone");
}
numerator = numerator + 1;
denominator = denominator +1;
System.out.println(dottedLine);
} // end of while
} // end of main
} // end of class
回答by HyperNeutrino
You can check for NaN by using var != var
. NaN
does not equal NaN
.
您可以使用var != var
. NaN
不等于NaN
。
EDIT: This is probably by far the worst method. It's confusing, terrible for readability, and overall bad practice.
编辑:这可能是迄今为止最糟糕的方法。它令人困惑,可读性很差,并且总体上是不好的做法。
回答by Grzegorz Gajos
You might want to consider also checking if a value is finite via Double.isFinite(value)
. Since Java 8 there is a new method in Double
class where you can check at once if a value is not NaN and infinity.
您可能还想考虑通过Double.isFinite(value)
. 从 Java 8 开始,Double
类中有一个新方法,您可以立即检查值是否不是 NaN 和无穷大。
/**
* Returns {@code true} if the argument is a finite floating-point
* value; returns {@code false} otherwise (for NaN and infinity
* arguments).
*
* @param d the {@code double} value to be tested
* @return {@code true} if the argument is a finite
* floating-point value, {@code false} otherwise.
* @since 1.8
*/
public static boolean isFinite(double d)
回答by Teela
The below code snippet will help evaluate primitive type holding NaN.
下面的代码片段将有助于评估持有 NaN 的原始类型。
double dbl = Double.NaN;
Double.valueOf(dbl).isNaN() ? true : false;
double dbl = Double.NaN;
Double.valueOf(dbl).isNaN() ? true : false;