欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
不要用float和double來(lái)進(jìn)行精確的小數計算 - Ye Yiliang‘s Blog - BlogJava

不要用floatdouble來(lái)進(jìn)行精確的小數計算

 

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

 

知其然:

為什么說(shuō)不能用 float double 來(lái)進(jìn)行精確小數計算呢?

試試執行這樣的語(yǔ)句:

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

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

你會(huì )發(fā)現結果和你的小學(xué)數學(xué)課程有沖突。

 

知其所以然

之所以出現這樣的奇怪答案,是因為 float double 不能精確的表達 0.1 ,或者任何 10 的負 n 次方。他們是設計來(lái)進(jìn)行科學(xué)和工程上的計算,提供精確的近似值的。它們在涉及金融方面的計算則是不在行的。因為金融方面要求絕對的精確。

 

解決方法

BigDecimal , int 或者 long 。

 

 

BigDecimal bd  =   new  BigDecimal( " 1.00 " );

 

 

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

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

如果小數位本身不長(cháng),則可以用 int 或者 long 來(lái)替代,我想你明白這個(gè)方法是什么意思的。在速度必須很快的位置,你又不介意自己看著(zhù)小數點(diǎn)位,這個(gè)還是可用的,但如果數字本身超過(guò)了 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 閱讀(461) 評論(3)  編輯 收藏 收藏至365Key 所屬分類(lèi): Java
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
系統設計 | 高精度計算
JAVA中控制double和float的精度
sqlserver與java數據類(lèi)型對應
java基礎知識講解(一)數據類(lèi)型和運算符
Java解決double和float精度不準的問(wèn)題
淺談java中的數學(xué)運算
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久