Pet*_*rey 561 java floating-point double rounding
在以下程序中,您可以看到每个值略小于.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);
}
Run Code Online (Sandbox Code Playgroud)
版画
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
Run Code Online (Sandbox Code Playgroud)
我正在使用Java 6更新31.
Oli*_*rth 572
摘要
在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
}
Run Code Online (Sandbox Code Playgroud)
这是因为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;
}
Run Code Online (Sandbox Code Playgroud)
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发现此信息)
3. http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round%28double%29
Sim*_*son 232
这似乎是一个已知的错误(Java bug 6430675:Math.round对0x1.fffffffffffffp-2有令人惊讶的行为)已在Java 7中得到修复.
Cha*_*har 83
JDK 6中的源代码:
public static long round(double a) {
return (long)Math.floor(a + 0.5d);
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
当值为0.49999999999999994d时,在JDK 6中,它将调用floor并因此返回1,但在JDK 7中,if条件是检查该数字是否是小于0.5的最大double值.在这种情况下,数字不是小于0.5的最大双精度值,因此该else块返回0.
您可以尝试0.49999999999999999d,它将返回1,但不返回0,因为这是小于0.5的最大双精度值.
Dan*_*lor 26
我在JDK 1.6 32位上也一样,但在Java 7 64位上,我得到0为0.49999999999999994,舍入为0,最后一行不打印.这似乎是一个VM问题,但是,使用浮点,您应该期望在各种环境(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
Run Code Online (Sandbox Code Playgroud)
shi*_*ail 11
以下的答案是Oracle 错误报告6430675的摘录.请访问该报告以获取完整说明.
方法{Math,StrictMath.round在操作上定义为
(long)Math.floor(a + 0.5d)
Run Code Online (Sandbox Code Playgroud)
用于双重参数.虽然这个定义通常按预期工作,但它给出了令人惊讶的结果,而不是0,对于0x1.fffffffffffffp-2(0.49999999999999994).
值0.49999999999999994是小于0.5的最大浮点值.作为十六进制浮点字面值,其值为0x1.fffffffffffffp-2,等于(2 - 2 ^ 52)*2 ^ -2.==(0.5 - 2 ^ 54).因此,总和的确切值
(0.5 - 2^54) + 0.5
Run Code Online (Sandbox Code Playgroud)
是1 - 2 ^ 54.这是两个相邻浮点数(1 - 2 ^ 53)和1之间的中间点.在IEEE 754算术轮到Java使用的最接近舍入模式时,当浮点结果不精确时,两者的距离越近必须返回包含确切结果的可表示浮点值; 如果两个值都相等,则返回其最后一位为零的值.在这种情况下,add的正确返回值是1,而不是小于1的最大值.
虽然该方法按照定义运行,但此输入的行为非常令人惊讶; 规范可以修改为更像"Round to the long long,rounding tied up",这将允许更改此输入的行为.