In Java 6 and previous, round(x) is executed as floor(x+0.5). This is a spec bug, for specifically this one neurotic case. Java 7 and above no longer mandates this broken implementation.
The problem
0.5+0.49999999999999994 is exactly 1 in dual accuracy:
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
}
This is happening because 0.49999999999999994 has a smaller index than 0.5, so when they're added, its mantissa is stirred, and the ULP gets bigger.
Solution:
After Java 7, OpenJDK (for example) executes it thus:
public static long round(double a) {
if (a != 0x1.fffffffffffffp-2) // biggest double value less than 0.5
return (long)floor(a + 0.5d);
else
return 0;
}