十進制與雙精度! -應該使用哪一個?何時使用? [重複]

本文翻譯自:decimal vs double! - Which one should I use and when? [duplicate]

This question already has an answer here: 這個問題已經在這裏有了答案:

I keep seeing people using doubles in C#. 我一直看到人們在C#中使用雙打。 I know I read somewhere that doubles sometimes lose precision. 我知道我在某個地方讀過一些內容,有時會失去精度。 My question is when should a use a double and when should I use a decimal type? 我的問題是什麼時候應該使用雙精度型,什麼時候應該使用十進制型? Which type is suitable for money computations? 哪種類型適合貨幣計算? (ie. greater than $100 million) (即超過1億美元)


#1樓

參考:https://stackoom.com/question/4tGb/十進制與雙精度-應該使用哪一個-何時使用-重複


#2樓

For money: decimal . 金錢: decimal It costs a little more memory, but doesn't have rounding troubles like double sometimes has. 它的費用多一點的內存,但沒有四捨五入的煩惱像double有時也有。


#3樓

For money, always decimal. 爲了錢, 總是十進制。 It's why it was created. 這就是爲什麼它被創建。

If numbers must add up correctly or balance, use decimal. 如果數字必須正確加總或保持平衡,請使用十進制。 This includes any financial storage or calculations, scores, or other numbers that people might do by hand. 這包括人們可能手工進行的任何財務存儲或計算,分數或其他數字。

If the exact value of numbers is not important, use double for speed. 如果數字的確切值不重要,請使用double作爲速度。 This includes graphics, physics or other physical sciences computations where there is already a "number of significant digits". 這包括圖形,物理學或其他物理科學計算,其中已經有“有效位數”。


#4樓

Definitely use integer types for your money computations. 絕對使用整數類型進行貨幣計算。 This cannot be emphasized enough, since at first glance it might seem that a floating point type is adequate. 由於乍看起來似乎浮點類型已足夠,因此不能足夠強調。

Here an example in python code: 這是python代碼中的示例:

>>> amount = float(100.00) # one hundred dollars
>>> print amount
100.0
>>> new_amount = amount + 1
>>> print new_amount
101.0
>>> print new_amount - amount
>>> 1.0

looks pretty normal. 看起來很正常。

Now try this again with 10^20 Zimbabwe dollars 現在再用10 ^ 20津巴布韋元嘗試一下

>>> amount = float(1e20)
>>> print amount
1e+20
>>> new_amount = amount + 1
>>> print new_amount
1e+20
>>> print new_amount-amount
0.0

As you can see, the dollar disappeared. 如您所見,美元消失了。

If you use the integer type, it works fine: 如果使用整數類型,則可以正常工作:

>>> amount = int(1e20)
>>> print amount
100000000000000000000
>>> new_amount = amount + 1
>>> print new_amount
100000000000000000001
>>> print new_amount - amount
1

#5樓

Decimal is for exact values. 小數表示精確值。 Double is for approximate values. Double是近似值。

USD: $12,345.67 USD (Decimal)
CAD: $13,617.27 (Decimal)
Exchange Rate: 1.102932 (Double)

#6樓

My question is when should a use a double and when should I use a decimal type? 我的問題是什麼時候應該使用雙精度型,什麼時候應該使用十進制型?

decimal for when you work with values in the range of 10^(+/-28) and where you have expectations about the behaviour based on base 10 representations - basically money. 當您使用10 ^(+/- 28)範圍內的值以及對基於基數10表示形式的行爲有期望的情況時,基本爲decimal

double for when you need relative accuracy (ie losing precision in the trailing digits on large values is not a problem) across wildly different magnitudes - double covers more than 10^(+/-300). double當你需要相對精確度(即在大的值的結尾數字失去精度不是問題)跨越很大的不同幅度- double覆蓋超過10 ^(+/- 300)。 Scientific calculations are the best example here. 科學計算是最好的例子。

which type is suitable for money computations? 哪種類型適合貨幣計算?

decimal, decimal , decimal 十進制, 十進制十進制

Accept no substitutes. 不接受替代品。

The most important factor is that double , being implemented as a binary fraction, cannot accurately represent many decimal fractions (like 0.1) at all and its overall number of digits is smaller since it is 64-bit wide vs. 128-bit for decimal . 最重要的因素是,以二進制分數形式實現的double根本無法準確表示許多decimal分數(例如0.1) 並且其總位數較小,因爲它的寬度爲64位,而decimal爲128位。 Finally, financial applications often have to follow specific rounding modes (sometimes mandated by law). 最後,財務申請通常必須遵循特定的舍入模式 (有時是法律規定的)。 decimal supports these ; decimal 支持這些 ; double does not. double沒有。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章