为什么Math.round(0.49999999999999994)返回1? [英] Why does Math.round(0.49999999999999994) return 1?

查看:238
本文介绍了为什么Math.round(0.49999999999999994)返回1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中,你可以看到每个小于 .5 的值被舍去,除了 0.5

pre $ 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);






$ b

  10.5圆整为11 
10.499999999999998圆整为10
圆整为10
9.499999999999998圆整为9
8.5圆整为9
8.499999999999998圆整是8
7.5圆整是8
7.499999999999999圆整是7
6.5圆整是7
6.499999999999999圆整是6
5.5圆整是6
5.499999999999999四舍五入5
4.5圆整为5
4.499999999999999圆整为4
3.5圆整为4
3.4999999999999996圆整为3
2.5圆整为3
2.4999999999999996圆整为2
1.5圆整为2
1.4999999999999998圆整为1
0.5圆整为1
0.49999999999999994圆整为1
0.4999999999999999圆整为0


我正在使用Java 6更新版本31.

解决方案

round(x) 被实现为 floor (x + 0.5) 1 这是一个规范错误,恰恰是这一个病态。 2 Java 7不再强制执行这个破坏的实现 3



问题

0.5+ 0.49999999999999994正好是1的双精度:

pre $静态无效打印(double d){
System.out.printf( %016x\\\
,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也变大了。
$ b

解决方案

从Java 7开始,OpenJDK(例如)就这样实现它:
$ b

  (double a){
if(a!= 0x1.fffffffffffffp-2)//最大double值小于0.5
return(long)floor(a + 0.5d);
else
return 0;

$ / code>







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找到这个)



$ sub
> 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/7 u40-b43 / java / lang / Math.java#Math.round%28double%29


In the following program you can see that each value slightly less than .5 is rounded down, except for 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);
}

prints

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

I am using Java 6 update 31.

解决方案

Summary

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

The problem

0.5+0.49999999999999994 is exactly 1 in double precision:

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 because 0.49999999999999994 has a smaller exponent than 0.5, so when they're added, its mantissa is shifted, and the ULP gets bigger.

The solution

Since Java 7, OpenJDK (for example) implements it thus: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 (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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆