Python+decimal完成精度計算的示例詳解
在進行小數(shù)計算的時候使用float,經常會出現(xiàn)小數(shù)位不精確的情況。在python編程中,推薦使用decimal來完成小數(shù)位的精度計算。
decimal是python中的標準庫,直接將Decimal導入到代碼塊中使用。
decimal意思為十進制,這個模塊提供了十進制浮點運算支持。通過幾個常見的實戰(zhàn)用例來說明一下其用法。
1. 浮點數(shù)轉Decimal
使用Decimal.from_float函數(shù)將隨便一個float類型的小數(shù)轉換成Decimal的數(shù)據(jù)類型,結果float類型數(shù)據(jù)就顯出原形了。
#?It?imports?the?Decimal?class?from?the?decimal?module. import?decimal from?decimal?import?Decimal #?It?converts?the?float?10.245?to?a?Decimal?object. decimal_?=?Decimal.from_float(10.245) print('浮點數(shù)轉為Decimal后:{0}'.format(decimal_)) #?浮點數(shù)轉為Decimal后:10.2449999999999992184029906638897955417633056640625
從結果來看,float浮點數(shù)轉換完成以后精度位數(shù)就變得很長不是原先的三位小數(shù)了,這是因為float浮點數(shù)本身就不精確轉換之后才會出現(xiàn)上面的效果。
2. Decimal除法設置
隨機選兩個整數(shù)將其定義為Decimal類型的數(shù)據(jù),之后對這兩個整個做除法通過保留相應的結果位數(shù)對其返回結果進行優(yōu)化。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace. from?decimal?import?* #?It?sets?the?precision?of?the?decimal?module?to?8. getcontext().prec?=?8 #?Dividing?1?by?6?and?storing?the?result?in?the?variable?`decimal_`. decimal_?=?Decimal(1)/Decimal(6) print('精確到8位小數(shù)后的結果是:{0}'.format(decimal_)) #?精確到8位小數(shù)后的結果是:0.16666667
很明顯做除法以后的結果應該是一個無限小數(shù),設置保留8位小數(shù)之后自動進行了四舍五入的計算得到0.16666667的結果。
3. Quantize設置結果
同樣是保留了兩位小數(shù),使用quantize函數(shù)能完成同樣的效果,默認結果也是經過了四舍五入的計算,若是想要固定小數(shù)位數(shù)使用此方法比較靠譜。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace. from?decimal?import?* #?Rounding?the?number?3.7829?to?two?decimal?places. decimal_?=?Decimal('3.7829').quantize(Decimal('0.00')) print('quantize設置保留兩位小數(shù)后的結果:{0}'.format(decimal_)) # quantize設置保留兩位小數(shù)后的結果:3.78
4. Decimal精度設置
這里還是做一個結果為無限小數(shù)的除法,分別使用向上取整、向下取整的方式保留一定位數(shù)的小數(shù)來說明問題。
一般情況下可能使用的都是向上取整,但是在一些領域比較金融、證券行業(yè)就必須采取向下取整的方式,首先來看一下常用的向上取整的方式來保留小數(shù)。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace. from?decimal?import?* #?It?sets?the?rounding?mode?to?ROUND_CEILING. getcontext().rounding?=?getattr(decimal,?'ROUND_CEILING') #?It?sets?the?precision?of?the?decimal?module?to?10. getcontext().prec?=?10 #?Converting?the?integer?9?to?a?string?and?then?converting?it?to?a?Decimal?object. decimal_?=?Decimal(1)?/?Decimal(str(9)) print('向上取整保留10位小數(shù):{0}'.format(decimal_.quantize(Decimal('0.0000000000')))) #?向上取整保留10位小數(shù):0.1111111112
這里有個問題就是,如果getcontext().prec已經設置小數(shù)位是10,那么在使用quantize函數(shù)固定小數(shù)位的時候就必須不超過10位才行,也就是不能超過有效位數(shù)否則就會報錯。
接下來看一下向下取整,向下取整的小數(shù)保留方式只需要修改getcontext().rounding的屬性為向下取整即可,為了對比結果我們還是采用同樣的數(shù)據(jù)來看看效果。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace. from?decimal?import?* #?It?sets?the?rounding?mode?to?ROUND_CEILING. getcontext().rounding?=?getattr(decimal,?'ROUND_FLOOR') #?It?sets?the?precision?of?the?decimal?module?to?10. getcontext().prec?=?10 #?Converting?the?integer?9?to?a?string?and?then?converting?it?to?a?Decimal?object. decimal_?=?Decimal(1)?/?Decimal(str(9)) print('向下取整保留10位小數(shù):{0}'.format(decimal_.quantize(Decimal('0.0000000000')))) #?向下取整保留10位小數(shù):0.1111111111
可以發(fā)現(xiàn)同樣的數(shù)據(jù)做除法,向下取整時結果是0.1111111111,向上取整時結果是0.1111111112。
同樣,還是很多的其他保留小數(shù)的方式可以使用比如四舍五入的方式,這也是很多很多行業(yè)會采取的一種小數(shù)保留的方式,再演示一下四舍五入時的保留小數(shù)。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace. from?decimal?import?* #?It?sets?the?rounding?mode?to?ROUND_HALF_UP. getcontext().rounding?=?getattr(decimal,?'ROUND_HALF_UP') #?It?sets?the?precision?of?the?decimal?module?to?4. getcontext().prec?=?5 #?It?converts?the?string?'3.14159'?to?a?Decimal?object. decimal_?=?Decimal('3.14159') print('四舍五入保留4位小數(shù):{0}'.format(decimal_.quantize(Decimal('0.0000')))) #?四舍五入保留4位小數(shù):3.1416
將3.14159通過四舍五入的方式保留4位小數(shù)之后就變成了3.1416,和我們預想的結果一樣。
到此這篇關于Python+decimal完成精度計算的示例詳解的文章就介紹到這了,更多相關Python decimal精度計算內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Python?Cupy模塊加速大規(guī)模數(shù)值計算實例深究
Cupy是一個基于NumPy的庫,專門設計用于在GPU上進行高性能計算,它提供了與NumPy相似的API,因此用戶可以很容易地將現(xiàn)有的NumPy代碼遷移到Cupy上,從而充分利用GPU的并行計算能力2023-12-12Python中棧、隊列與優(yōu)先級隊列的實現(xiàn)方法
這篇文章主要給大家介紹了關于Python中棧、隊列與優(yōu)先級隊列的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06TensorFlow實現(xiàn)非線性支持向量機的實現(xiàn)方法
本篇文章主要介紹了TensorFlow實現(xiàn)非線性支持向量機的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04