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

為什么 Math.round(0.49999999999999994) 返回 1?

Why does Math.round(0.49999999999999994) return 1?(為什么 Math.round(0.49999999999999994) 返回 1?)
本文介紹了為什么 Math.round(0.49999999999999994) 返回 1?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在下面的程序中你可以看到每個略小于.5的值都被四舍五入了,除了0.5.

In the following program you can see that each value slightly less than .5 is rounded down, except for 0.5.

for (int i = 10; i >= 0; i--) {
    long l = Double.doubleToLongBits(i + 0.5);
    double x;
    do {
        x = Double.longBitsToDouble(l);
        System.out.println(x + " rounded is " + Math.round(x));
        l--;
    } while (Math.round(x) > i);
}

打印

10.5 rounded is 11
10.499999999999998 rounded is 10
9.5 rounded is 10
9.499999999999998 rounded is 9
8.5 rounded is 9
8.499999999999998 rounded is 8
7.5 rounded is 8
7.499999999999999 rounded is 7
6.5 rounded is 7
6.499999999999999 rounded is 6
5.5 rounded is 6
5.499999999999999 rounded is 5
4.5 rounded is 5
4.499999999999999 rounded is 4
3.5 rounded is 4
3.4999999999999996 rounded is 3
2.5 rounded is 3
2.4999999999999996 rounded is 2
1.5 rounded is 2
1.4999999999999998 rounded is 1
0.5 rounded is 1
0.49999999999999994 rounded is 1
0.4999999999999999 rounded is 0

我正在使用 Java 6 更新 31.

I am using Java 6 update 31.

推薦答案

總結

在 Java 6(可能更早)中,round(x) 被實現為 floor(x+0.5).1 這是一個規范錯誤,正是針對這種病態的情況.2Java 7 不再強制執行這種損壞的實現.3

In Java 6 (and presumably earlier), round(x) is implemented as floor(x+0.5).1 This is a specification bug, for precisely this one pathological case.2 Java 7 no longer mandates this broken implementation.3

問題

0.5+0.49999999999999994 在雙精度中正好是 1:

0.5+0.49999999999999994 is exactly 1 in double precision:

static void print(double d) {
    System.out.printf("%016x
", Double.doubleToLongBits(d));
}

public static void main(String args[]) {
    double a = 0.5;
    double b = 0.49999999999999994;

    print(a);      // 3fe0000000000000
    print(b);      // 3fdfffffffffffff
    print(a+b);    // 3ff0000000000000
    print(1.0);    // 3ff0000000000000
}

這是因為 0.49999999999999994 的指數小于 0.5,所以當它們相加時,它的尾數會移動,ULP 會變大.

This is because 0.49999999999999994 has a smaller exponent than 0.5, so when they're added, its mantissa is shifted, and the ULP gets bigger.

解決方案

從 Java 7 開始,OpenJDK(例如)這樣實現它:4

Since Java 7, OpenJDK (for example) implements it thus:4

public static long round(double a) {
    if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
        return (long)floor(a + 0.5d);
    else
        return 0;
}

<小時>

<子>1. http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round%28double%29

<子>2. http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6430675(感謝@SimonNickerson 找到這個)

2. http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6430675 (credits to @SimonNickerson for finding this)

<子>3. http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round%28double%29

<子>4. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/lang/Math.java#Math.round%28double%29

這篇關于為什么 Math.round(0.49999999999999994) 返回 1?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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.com在线观看 | 成人性生交大片 | 日日操夜夜摸 | 四虎最新视频 | 国产日韩精品在线 | 欧美日韩国产一区二区三区 | 久草青青草| 伊人伊人伊人 | 色伊人网 | 欧美综合一区 | 国产一区二区三区在线免费 | 国外成人在线视频 | 国产精品大片在线观看 | 亚洲欧美中文字幕在线观看 | 国产精品永久免费视频 | 亚洲欧洲中文日韩 | 国产在线一区二区三区 | va精品| 在线播放中文字幕 | 欧美1区2区 | 久久国际精品 | 欧美一级特黄aaa大片在线观看 | 91精品国产乱码久久久久久久久 | 日韩在线视频一区二区三区 | 四虎在线视频 | 欧美男人天堂 | 国产精品嫩草影院精东 | 精品国产一区二区三区久久 | 国产精品久久久久久久久久妞妞 | 狠狠婷婷综合久久久久久妖精 | 久久国内 | 午夜视频精品 | 插插插干干干 | 亚洲人成人一区二区在线观看 | 国产精品久久久乱弄 | 日本一区二区视频 | 国产激情综合五月久久 |