places - java round up




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

JDK 6中的源代碼:

public static long round(double a) {
    return (long)Math.floor(a + 0.5d);
}

JDK 7中的源代碼:

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

當值為0.49999999999999994d時,在JDK 6中,它將調用floor並因此返回1,但在JDK 7中, if條件是檢查該數是否是小於0.5的最大double值。 在這種情況下,數字不是小於0.5的最大double值,所以else塊返回0。

您可以嘗試0.49999999999999999d,這將返回1,但不是0,因為這是最大的double值,小於0.5。

在下面的程序中,您可以看到每個略小於.5值都向下舍入,除了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。


概要

在Java 6中(推測更早), round(x)被實現為floor(x+0.5)1這是一個規範錯誤,正是為了這一個病態。 2 Java 7不再強制要求這個破壞的實現。 3

問題

0.5 + 0.49999999999999994在雙精度中恰好為1:

static void print(double d) {
    System.out.printf("%016x\n", 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會變大。

解決方案

從Java 7開始,OpenJDK(例如)就這樣實現它: 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找到這個)

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

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


我在JDK 1.6 32位上也有相同的結果,但在Java 7 64位上,0.49999999999999994的值為0,其舍入為0,最後一行不打印。 這似乎是一個虛擬機問題,但是,使用浮點數,你應該期望結果在各種環境(CPU,32位或64位模式)上有所不同。

而且,當使用round或反轉矩陣等時,這些可以產生巨大的差異。

x64輸出:

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 0





rounding