Python+decimal完成精度計算的示例詳解
在進行小數(shù)計算的時候使用float,經(jīng)常會出現(xiàn)小數(shù)位不精確的情況。在python編程中,推薦使用decimal來完成小數(shù)位的精度計算。
decimal是python中的標準庫,直接將Decimal導入到代碼塊中使用。
decimal意思為十進制,這個模塊提供了十進制浮點運算支持。通過幾個常見的實戰(zhàn)用例來說明一下其用法。
1. 浮點數(shù)轉(zhuǎn)Decimal
使用Decimal.from_float函數(shù)將隨便一個float類型的小數(shù)轉(zhuǎn)換成Decimal的數(shù)據(jù)類型,結(jié)果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ù)轉(zhuǎn)為Decimal后:{0}'.format(decimal_))
#?浮點數(shù)轉(zhuǎn)為Decimal后:10.2449999999999992184029906638897955417633056640625
從結(jié)果來看,float浮點數(shù)轉(zhuǎn)換完成以后精度位數(shù)就變得很長不是原先的三位小數(shù)了,這是因為float浮點數(shù)本身就不精確轉(zhuǎn)換之后才會出現(xiàn)上面的效果。
2. Decimal除法設(shè)置
隨機選兩個整數(shù)將其定義為Decimal類型的數(shù)據(jù),之后對這兩個整個做除法通過保留相應的結(jié)果位數(shù)對其返回結(jié)果進行優(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ù)后的結(jié)果是:{0}'.format(decimal_))
#?精確到8位小數(shù)后的結(jié)果是:0.16666667
很明顯做除法以后的結(jié)果應該是一個無限小數(shù),設(shè)置保留8位小數(shù)之后自動進行了四舍五入的計算得到0.16666667的結(jié)果。
3. Quantize設(shè)置結(jié)果
同樣是保留了兩位小數(shù),使用quantize函數(shù)能完成同樣的效果,默認結(jié)果也是經(jīng)過了四舍五入的計算,若是想要固定小數(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è)置保留兩位小數(shù)后的結(jié)果:{0}'.format(decimal_))
# quantize設(shè)置保留兩位小數(shù)后的結(jié)果:3.78
4. Decimal精度設(shè)置
這里還是做一個結(jié)果為無限小數(shù)的除法,分別使用向上取整、向下取整的方式保留一定位數(shù)的小數(shù)來說明問題。
一般情況下可能使用的都是向上取整,但是在一些領(lǐng)域比較金融、證券行業(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已經(jīng)設(shè)置小數(shù)位是10,那么在使用quantize函數(shù)固定小數(shù)位的時候就必須不超過10位才行,也就是不能超過有效位數(shù)否則就會報錯。
接下來看一下向下取整,向下取整的小數(shù)保留方式只需要修改getcontext().rounding的屬性為向下取整即可,為了對比結(jié)果我們還是采用同樣的數(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ù)做除法,向下取整時結(jié)果是0.1111111111,向上取整時結(jié)果是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,和我們預想的結(jié)果一樣。
到此這篇關(guān)于Python+decimal完成精度計算的示例詳解的文章就介紹到這了,更多相關(guān)Python decimal精度計算內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python內(nèi)置的HTTP協(xié)議服務器SimpleHTTPServer使用指南
這篇文章主要介紹了Python內(nèi)置的HTTP協(xié)議服務器SimpleHTTPServer使用指南,SimpleHTTPServer本身的功能十分簡單,文中介紹了需要的朋友可以參考下2016-03-03
python虛擬環(huán)境virtualenv的使用教程
本篇文章主要介紹了python虛擬環(huán)境virtualenv的使用教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
詳解如何使用python實現(xiàn)猜數(shù)字游戲
“猜數(shù)字”游戲是一款簡單而有趣的小游戲,玩家需要在給定的范圍內(nèi)猜出一個由計算機隨機生成的數(shù)字,本文將使用Python語言來實現(xiàn)這款游戲,并詳細介紹其實現(xiàn)過程,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下2024-04-04
python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
這篇文章主要介紹了python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
python非對稱加密算法RSA實現(xiàn)原理與應用詳解
RSA加密算法是一種非對稱加密算法,RSA算法的安全性基于大數(shù)分解的困難性,即已知兩個大素數(shù)p和q的乘積n,求解p和q非常困難,RSA算法廣泛應用于數(shù)據(jù)加密和數(shù)字簽名等領(lǐng)域,本文將詳細介紹如何在Python中使用RSA算法進行加密和解密,需要的朋友可以參考下2024-09-09

