第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么Math.round(0.49999999999999994)返回1?

為什么Math.round(0.49999999999999994)返回1?

在下面的程序中,您可以看到.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 update 31。
查看完整描述

3 回答

?
守候你守候我

TA貢獻1802條經(jīng)驗 獲得超10個贊

JDK 6中的源代碼:


public static long round(double a) {

    return (long)Math.floor(a + 0.5d);

}

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;

    }

}

當值為0.49999999999999994d時,在JDK 6中,它將調(diào)用floor,因此返回1,但在JDK 7中,if條件是檢查該數(shù)字是否為小于0.5的最大double值。由于在這種情況下,該數(shù)字不是小于0.5的最大double值,因此該else塊返回0。


您可以嘗試0.49999999999999999d,它將返回1,但不會返回0,因為這是小于0.5的最大double值。


查看完整回答
反對 回復(fù) 2019-10-04
?
慕的地8271018

TA貢獻1796條經(jīng)驗 獲得超4個贊

我在32位的JDK 1.6上具有相同的功能,但是在Java 7 64位的上,對于0.49999999999999994則具有0,其四舍五入為0,并且不打印最后一行。這似乎是VM的問題,但是,使用浮點運算,您應(yīng)該期望在各種環(huán)境(CPU,32位或64位模式)下結(jié)果會有所不同。


并且,當使用round或求逆矩陣等時,這些位會產(chǎn)生很大的不同。


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


查看完整回答
反對 回復(fù) 2019-10-04
  • 3 回答
  • 0 關(guān)注
  • 899 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號