<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 17  文章 - 49  trackbacks - 0
    <2006年9月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    1234567

    常用鏈接

    留言簿(1)

    隨筆分類(17)

    隨筆檔案(17)

    相冊

    最新隨筆

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    ?

    2006 913 星期三

    不要用floatdouble來進(jìn)行精確的小數(shù)計(jì)算

    ?

    什么?難道它們不就是為了小數(shù)計(jì)算而生的么?在我看到 effective java item31 的時候,發(fā)出了這個孤陋寡聞的疑問。

    ?

    知其然:

    為什么說不能用 float double 來進(jìn)行精確小數(shù)計(jì)算呢?

    試試執(zhí)行這樣的語句:

    System.out.println( 1.03 ? - ?. 42 ); // 答案是0.6100000000000001?!

    System.out.println(
    1.00 ? - ? 9 * . 10 ); // 答案是0.09999999999999995?!

    你會發(fā)現(xiàn)結(jié)果和你的小學(xué)數(shù)學(xué)課程有沖突。

    ?

    知其所以然

    之所以出現(xiàn)這樣的奇怪答案,是因?yàn)?/span> float double 不能精確的表達(dá) 0.1 ,或者任何 10 的負(fù) n 次方。他們是設(shè)計(jì)來進(jìn)行科學(xué)和工程上的計(jì)算,提供精確的近似值的。它們在涉及金融方面的計(jì)算則是不在行的。因?yàn)榻鹑诜矫嬉蠼^對的精確。

    ?

    解決方法

    BigDecimal int 或者 long

    ?

    ?

    BigDecimal?bd? = ? new ?BigDecimal( " 1.00 " );

    ?

    ?

    把所有牽涉到的小數(shù)都以這種方式轉(zhuǎn)變?yōu)?/span> BigDecimal 對象,然后用它提供的方法進(jìn)行計(jì)算。

    你就得到了精確的計(jì)算結(jié)果,隨之而來有兩個小缺點(diǎn),一個。。。很顯然,就是比直接用原始類型要麻煩,也不能用 +,-,*,/ 這些直觀的符號了;第二個就是速度會慢一點(diǎn),如果不是用于大量的循環(huán)之中,則不是那么要緊。不過,你也同時得到了一個好處, BigDecimal 類還帶了豐富的舍入方法,也是不錯的。

    如果小數(shù)位本身不長,則可以用 int 或者 long 來替代,我想你明白這個方法是什么意思的。在速度必須很快的位置,你又不介意自己看著小數(shù)點(diǎn)位,這個還是可用的,但如果數(shù)字本身超過了 18 位,就只能用 BigDecimal 了。

    ?

    Wednesday, September 13, 2006

    Don’t use float and double when exact answers are required

    ?

    What? Aren’t calculating decimal are what they are design for? I made such an ignorant question when I first read effective java-item31.

    ?

    The phenomenon:

    Why we can’t use float and double to calculate decimal when exact results are required?

    Try the following statements:

    ?

    System.out.println( 1.03 ? - ?. 42 ); // the?answer?is?0.6100000000000001?!

    System.out.println(
    1.00 ? - ? 9 * . 10 ); // the?answer?is?0.09999999999999995?!

    ?

    You will find the answers are not agree with what you have learned in primary school.

    ?

    The reason:

    It is all because float and double can not represent exactly 0.1, or any other negative power of ten. They are designed for scientific and engineering calculation which demands accurate approximation. And they are ill-suited for monetary calculations.

    ?

    The solution:

    Use BigDecimal, int or long instead.

    ?

    ?

    BigDecimal?bd? = ? new ?BigDecimal( " 1.00 " );

    ?

    ?

    Transfer all decimal numbers involved in the calcution into BigDecimal objects as above, and then use them to calcute for a exact result, following with two disadvantages, the first, obviously, less convenient than using primitive types, all notations like +,-,*,/ can not be applied neither; the second, it will be slower, which can be ignored if it was not used in a heavily repeated loop. But you also get one merit which comes from the fact that BigDecimal carrys quite a lot of rounding methods.

    If the quantity is not big, you can use int or long instead, you surely understand how to implement the idea. In performance critical section, and you don’t mind keeping track the of the decimal point yourself, this is feasible. But you have not choice if the quantity is over 18 digits, only BigDecimal is available.

    posted on 2006-09-13 19:23 Ye Yiliang 閱讀(8738) 評論(4)  編輯  收藏 所屬分類: Java

    FeedBack:
    # re: 不要用float和double來進(jìn)行精確的小數(shù)計(jì)算 2006-09-14 00:21 weide
    從寫Dephi的時候就發(fā)現(xiàn)了這個問題,尤其在比較大小的時候讓人相當(dāng)困惑

    然后用C#寫程序的時候,開始全用了Decimal類型,但是這個計(jì)算過程也是相當(dāng)?shù)姆爆崳袛?shù)據(jù)在運(yùn)算之間都得轉(zhuǎn)成double,因?yàn)?net提供的數(shù)學(xué)函數(shù)的參數(shù)都是double(或者有不是的,我還沒找到)

    后來又花了很大的功夫全變成double了,慘啊-__-|||

    另外就是如何確定有效位數(shù)的問題,如果不做取舍,常常會搞得數(shù)據(jù)超長,當(dāng)參與計(jì)算的數(shù)據(jù)多起來的時候,就不知道多少是有效的數(shù)據(jù)了:(

    樓主可有解決辦法?  回復(fù)  更多評論
      
    # re: 不要用float和double來進(jìn)行精確的小數(shù)計(jì)算 2006-09-14 08:42 Flyingis
    @weide
    Decimal的構(gòu)造方法應(yīng)該可以直接設(shè)定有效位數(shù)啊。

    關(guān)于這個問題,在《Java Puzzlers》上面也有詳細(xì)的闡述。  回復(fù)  更多評論
      
    # re: 不要用float和double來進(jìn)行精確的小數(shù)計(jì)算 2006-09-14 13:06 lgn21st
    《Java Puzzle》上面開篇就提到這個puzzle!  回復(fù)  更多評論
      
    # re: 不要用float和double來進(jìn)行精確的小數(shù)計(jì)算 2006-09-14 18:54 冰川
    沒錯!
    金融方面的軟件一般都用BigDecimal.  回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲国产婷婷综合在线精品| 亚洲精品美女久久777777| 免费国产草莓视频在线观看黄| 亚洲中文久久精品无码| 蜜桃成人无码区免费视频网站 | 亚洲色四在线视频观看| 三年片在线观看免费观看高清电影 | 国产精品无码免费播放| 一级毛片在线免费视频| 亚洲精品网站在线观看你懂的| 免费无码又爽又刺激高潮的视频 | MM1313亚洲国产精品| 亚洲熟妇无码乱子AV电影| 久久福利资源网站免费看| 一级做a爰片久久毛片免费陪 | 毛片免费视频在线观看| 韩国免费a级作爱片无码| 亚洲www在线观看| 亚洲色自偷自拍另类小说| 毛片免费在线播放| 成人爽a毛片免费| 狠狠综合亚洲综合亚洲色| 久久久亚洲AV波多野结衣| 亚洲国产中文v高清在线观看| 无码区日韩特区永久免费系列 | 亚洲AV无码乱码精品国产| 91精品手机国产免费| 丰满少妇作爱视频免费观看| 亚洲人配人种jizz| 亚洲爱情岛论坛永久| 国产精品亚洲w码日韩中文| 无码永久免费AV网站| 日韩精品无码一区二区三区免费| 国产AV无码专区亚洲AV麻豆丫| 精品亚洲成a人片在线观看| 亚洲一区视频在线播放| 天天看免费高清影视| 欧洲乱码伦视频免费| 6080午夜一级毛片免费看| 野花香高清在线观看视频播放免费| 男女猛烈xx00免费视频试看|