如何测试 double 是否等于 NaN?

发布于 2024-08-05 04:27:39 字数 50 浏览 12 评论 0原文

我在 Java 中有一个 double,我想检查它是否为 NaN。 最好的方法是什么?

I have a double in Java and I want to check if it is NaN.
What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冰雪之触 2024-08-12 04:27:39

使用静态 Double .isNaN(double) 方法,或者您的 Double.isNaN() 方法。

// 1. static method
if (Double.isNaN(doubleValue)) {
    ...
}
// 2. object's method
if (doubleObject.isNaN()) {
    ...
}

简单地这样做:

if (var == Double.NaN) {
    ...
}

还不够,因为 IEEE 对于 NaN 和浮点数的标准 已定义。

Use the static Double.isNaN(double) method, or your Double's .isNaN() method.

// 1. static method
if (Double.isNaN(doubleValue)) {
    ...
}
// 2. object's method
if (doubleObject.isNaN()) {
    ...
}

Simply doing:

if (var == Double.NaN) {
    ...
}

is not sufficient due to how the IEEE standard for NaN and floating point numbers is defined.

一直在等你来 2024-08-12 04:27:39

尝试 Double.isNaN ()

如果此 Double 值是非数字 (NaN),则返回 true,否则返回 false。

请注意,[double.isNaN()] 将不起作用,因为未装箱的双精度数没有与其关联的方法。

Try Double.isNaN():

Returns true if this Double value is a Not-a-Number (NaN), false otherwise.

Note that [double.isNaN()] will not work, because unboxed doubles do not have methods associated with them.

天生の放荡 2024-08-12 04:27:39

您可能还需要考虑通过 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)

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.

/**
 * 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)
懒猫 2024-08-12 04:27:39

您可以使用 var != var 检查 NaN。 NaN 不等于 NaN

编辑:这可能是迄今为止最糟糕的方法。它令人困惑,可读性差,而且总体上是不好的做法。

You can check for NaN by using var != var. NaN does not equal NaN.

EDIT: This is probably by far the worst method. It's confusing, terrible for readability, and overall bad practice.

∝单色的世界 2024-08-12 04:27:39

如果您的测试值是Double(不是基元)并且可能是null(这显然也不是数字),那么您应该使用以下术语:

< code>(value==null || Double.isNaN(value))

因为 isNaN() 想要一个基元(而不是将任何基元双精度装箱为 Double)Double),传递 null 值(无法拆箱为 Double)将导致异常,而不是预期的 false

If your value under test is a Double (not a primitive) and might be null (which is obviously not a number too), then you should use the following term:

(value==null || Double.isNaN(value))

Since isNaN() wants a primitive (rather than boxing any primitive double to a Double), passing a null value (which can't be unboxed to a Double) will result in an exception instead of the expected false.

2024-08-12 04:27:39

下面的代码片段将帮助评估持有 NaN 的原始类型。

双 dbl = Double.NaN;
Double.valueOf(dbl).isNaN() ?真:假;

The below code snippet will help evaluate primitive type holding NaN.

double dbl = Double.NaN;
Double.valueOf(dbl).isNaN() ? true : false;

回首观望 2024-08-12 04:27:39

初学者需要实际例子。所以尝试下面的代码。

public class Not_a_Number {

public static void main(String[] args) {
    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

Beginners needs practical examples. so try the following code.

public class Not_a_Number {

public static void main(String[] args) {
    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文