久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 Java 的 Double.compare(double, double) 是這樣實現

Why is Java#39;s Double.compare(double, double) implemented the way it is?(為什么 Java 的 Double.compare(double, double) 是這樣實現的?)
本文介紹了為什么 Java 的 Double.compare(double, double) 是這樣實現的?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在查看 compare(double, double) 在 Java 標準庫 (6) 中.上面寫著:

I was looking at the implementation of compare(double, double) in the Java standard library (6). It reads:

public static int compare(double d1, double d2) {
    if (d1 < d2)
        return -1;       // Neither val is NaN, thisVal is smaller
    if (d1 > d2)
        return 1;        // Neither val is NaN, thisVal is larger

    long thisBits = Double.doubleToLongBits(d1);
    long anotherBits = Double.doubleToLongBits(d2);

    return (thisBits == anotherBits ?  0 : // Values are equal
            (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
             1));                          // (0.0, -0.0) or (NaN, !NaN)
}

這個實現的優點是什么?

What are the merits of this implementation?

優點"是一個(非常)糟糕的詞選擇.我想知道這是如何工作的.

edit: "Merits" was a (very) bad choice of words. I wanted to know how this works.

推薦答案

@Shoover 的答案是正確的(閱讀!),但除此之外還有更多內容.

@Shoover's answer is correct (read it!), but there is a bit more to it than this.

作為 javadoc for Double::equals 狀態:

As the javadoc for Double::equals states:

這個定義允許哈希表正常運行."

"This definition allows hash tables to operate properly."

假設 Java 設計者決定用與 ==<相同的語義來實現 equals(...)compare(...)/code> 在包裝的 double 實例上.這意味著 equals() 將始終為包裝的 NaN 返回 false.現在考慮如果您嘗試在 Map 或 Collection 中使用包裝的 NaN 會發生什么.

Suppose that the Java designers had decided to implement equals(...) and compare(...) with the same semantics as == on the wrapped double instances. This would mean that equals() would always return false for a wrapped NaN. Now consider what would happen if you tried to use a wrapped NaN in a Map or Collection.

List<Double> l = new ArrayList<Double>();
l.add(Double.NaN);
if (l.contains(Double.NaN)) {
    // this wont be executed.
}

Map<Object,String> m = new HashMap<Object,String>();
m.put(Double.NaN, "Hi mum");
if (m.get(Double.NaN) != null) {
    // this wont be executed.
}

這樣做沒有多大意義!

可能存在其他異常,因為 -0.0+0.0 具有不同的位模式,但根據 == 是相等的.

Other anomalies would exist because -0.0 and +0.0 have different bit patterns but are equal according to ==.

因此,Java 設計者決定(正確地 IMO)為我們今天擁有的這些 Double 方法采用更復雜(但更直觀)的定義.

So the Java designers decided (rightly IMO) on the more complicated (but more intuitive) definition for these Double methods that we have today.

這篇關于為什么 Java 的 Double.compare(double, double) 是這樣實現的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲成人av | 日日干日日射 | 欧美专区在线 | 成年人的视频免费观看 | 国产免费播放视频 | 一级毛片色一级 | 日韩视频三区 | 91视频一区二区 | 成人午夜毛片 | 成人av一区二区三区 | 免费黄色日本 | 国产亚洲一区二区三区在线 | 中文字幕精品一区 | 欧美日韩三级 | 91av视频在线免费观看 | 国产激情视频在线 | 日韩一区二区三区在线 | 欧美中文字幕一区 | 色天天综合 | 欧美天堂 | 国产精品99久久久久久宅男 | 国产欧美一区二区三区日本久久久 | 五月婷婷色 | 久久精品色视频 | 国产精品人人做人人爽 | 国产精品美女www爽爽爽 | 在线播放亚洲 | 亚洲九九 | 九九热在线视频观看这里只有精品 | 日韩欧美在线视频 | 免费观看黄色片视频 | 人人爽人人草 | 韩国精品一区 | 国产高清美女一级a毛片久久w | 羞羞网站在线观看 | 亚洲成人自拍 | www.国产| 精品久久久久久久久久久 | 国产精品成人一区二区 | 精品欧美一区二区三区免费观看 | 亚洲精品久久久一区二区三区 |