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

在下面的程序中,您可以看到每个略小于.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不再强制执行这个破坏的实现。

问题

0.5 + 0.49999999999999994在双精度中恰好为1:

static void print(double d) {
    System.out.printf("%016xn", 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


这似乎是一个已知错误(Java bug 6430675:Math.round对于0x1.fffffffffffffp-2具有令人惊讶的行为),这在Java 7中已得到修复。


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。

链接地址: http://www.djcxy.com/p/14987.html

上一篇: Why does Math.round(0.49999999999999994) return 1?

下一篇: Denormalized floating point in Objective