问题描述
在下面的程序中你可以看到每个略小于.5
的值都被四舍五入了,除了0.5
.
In the following program you can see that each value slightly less than .5
is rounded down, except for 0.5
.
打印
我正在使用 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:
这是因为 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
<小时>
<子>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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!