简体   繁体   English

Double.NaN是一个对象

[英]Double.NaN is an object

public class Double1 {    

    public static double parseDouble(String _s, double _def) {
        try {
            return Double.parseDouble(_s);
        }
        catch(Exception e) {
        }
        return _def;
    }

    public static void main(String[] args) {
        Double1 db=new Double1();
        boolean ab=db.parseDouble("vijay", Double.NaN)!=Double.NaN?true:false;
        System.out.println("ab value: "+ ab);
        System.out.println(Double.NaN==Double.NaN);
    }
}

It should return true where as the above code returns false . 当上面的代码返回false ,它应该返回true Why? 为什么?

NaN's compare false to everything - including themselves. NaN比较所有事物(包括他们自己)都是错误的。

You can check for NaN with 您可以使用以下方法检查NaN

Double.isNaN(doubleValue)

Which actually does nothing other than using exactly this behavior: A value x is a NaN if x != x . 实际上,除了完全使用此行为外,什么也没做:如果x != x则值x是NaN。

but it's normal. 但这是正常的。 Your parseDouble method tries to parse "vijay" and returns _def because "vijay" is not a double value.And db.parseDouble("vijay", Double.NaN) will return Double.NaN an finally Double.NaN!=Double.NaN is false. 您的parseDouble方法尝试解析“ vijay”并返回_def,因为“ vijay”不是双精度值。而db.parseDouble("vijay", Double.NaN)将返回Double.NaN ,最后是Double.NaN!=Double.NaN是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM