遊客:  註冊 | 登錄 | 幫助





標題: [討論] Java computation bug
  stanfish     Rank: 2Rank: 2
藍之初
性別 保密
UID 11127

精華 0
帖子 9
積分 100   詳情

閱讀權限 30
註冊 2006-9-4
來自
狀態 離線

 
 
 
 
發表於 2006-10-10 12:05 PM  資料  個人空間  短訊  加為好友 
Java computation bug

I tried System.out.println("1.1+1.01-2.11="+(1.1+1.01-2.11));  recently.  Guess what it comes out! The answer is not zero, but a number very close to zero.  I guess it is computation gap in the double type, but I am not sure... Anyone noticed that before or know what is going on? Thanks!

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2006-10-11 02:59 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 stanfish 於 2006-10-10 12:05 AM 發表
I tried System.out.println("1.1+1.01-2.11="+(1.1+1.01-2.11));  recently.  Guess what it comes out! The answer is not zero, but a number very close to zero.  I guess it is computation gap  ...

I think that's the nature of float or double data type calculation.  You will have to do some roundings to get the result right.  If you really care about the precision, you might want to use BigDecimal.

The reasons why the calculation of float/double values does not yield to accurate result involves how float/double are stored in memory and more.  I think you can probably find some explanation on the web if you are interested.

頂部



快速美言
           


當前時區 GMT+8, 現在時間是 2024-11-27 03:02 AM

    Powered by Discuz!  © 2001-2007 Comsenz Inc.   
Processed in 0.016620 second(s), 9 queries

清除 Cookies - 聯繫我們 - LIPS Corner 新天藍 - Archiver