在下面的程序中,你可以看到每个值略小于.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 1110.499999999999998 rounded is 109.5 rounded is 109.499999999999998 rounded is 98.5 rounded is 98.499999999999998 rounded is 87.5 rounded is 87.499999999999999 rounded is 76.5 rounded is 76.499999999999999 rounded is 65.5 rounded is 65.499999999999999 rounded is 54.5 rounded is 54.499999999999999 rounded is 43.5 rounded is 43.4999999999999996 rounded is 32.5 rounded is 32.4999999999999996 rounded is 21.5 rounded is 21.4999999999999998 rounded is 10.5 rounded is 10.49999999999999994 rounded is 10.4999999999999999 rounded is 0我正在使用Java 6。
相似问题