我有奇怪的十進制計算,真的讓我感到驚訝,我有兩個大的小數,一個是正常的程序,另一個是要約價格。然後,我想計算折扣百分比並將其上限爲最接近的整數。在Java中奇怪的十進制計算,可能是一個錯誤?
這裏是代碼:
BigDecimal listPrice = new BigDecimal(510000);
BigDecimal offerPrice = new BigDecimal(433500);
int itemDiscount = (int)Math.ceil(((listPrice.longValue() - offerPrice.longValue())/(float) listPrice.longValue()) * 100);
我期望它設置爲15的itemDiscount價值,但意外的是有16個,哇。然後,我打印每個計算以表示語句的問題,所以我把的System.out.println每條語句如下:
System.out.println(listPrice.longValue() - offerPrice.longValue()); //==> show 76500
System.out.println((listPrice.longValue() - offerPrice.longValue())/(float) listPrice.longValue()); // ==> 0.15
System.out.println((listPrice.longValue() - offerPrice.longValue()) * 100/(float) listPrice.longValue()); // ==> 15.000001
問題是在上面的語句,返回15.0 istead,它返回15.000001 。當我ceil它,它當然會返回16而不是15.
如果這種情況是什麼解釋?這是它的方式還是它是一個錯誤?
啊,浮點算術的美。 –
IEEE-754反擊。 –
@EdwinDalorzo - 這不是IEE-754的錯。每個有限的浮點表示都會在某個點出現舍入誤差的問題。 –