久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 中文字幕 在线观看 | 一区二区三区欧美在线观看 | 久视频在线观看 | 视频在线一区二区 | 精品亚洲一区二区三区 | 91污在线 | 91精品国产91综合久久蜜臀 | www国产成人免费观看视频,深夜成人网 | 盗摄精品av一区二区三区 | 久久久日韩精品一区二区三区 | 精品中文字幕在线 | 久久91精品| 一本一道久久a久久精品综合蜜臀 | 亚洲欧美激情视频 | 91pao对白在线播放 | 日本一区二区三区四区 | 国产精品永久免费视频 | 成人日批视频 | 久久一区二区三区四区五区 | 国产一区2区 | 欧美日韩视频一区二区 | 视频精品一区二区三区 | 欧美精品一区二区三区四区五区 | 日本一二区视频 | 午夜成人免费视频 | 国产精品久久久久久影院8一贰佰 | 亚洲成人av一区二区 | 精品国产一区二区三区成人影院 | 国产激情在线 | 亚洲精品乱码久久久久久蜜桃91 | 91在线观| 中文字幕在线观看一区 | 一区二区三区影院 | 久久aⅴ乱码一区二区三区 亚洲国产成人精品久久久国产成人一区 | 综合色在线 | 欧美视频在线免费 | 欧美在线一区二区三区 | 欧美一级免费黄色片 | 综合久| 97精品超碰一区二区三区 | 亚洲网址 |