日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python+decimal完成精度計算的示例詳解_python

作者:Sir?老王 ? 更新時間: 2022-11-27 編程語言

在進行小數計算的時候使用float,經常會出現小數位不精確的情況。在python編程中,推薦使用decimal來完成小數位的精度計算。

decimal是python中的標準庫,直接將Decimal導入到代碼塊中使用。

decimal意思為十進制,這個模塊提供了十進制浮點運算支持。通過幾個常見的實戰用例來說明一下其用法。

1.?浮點數轉Decimal

使用Decimal.from_float函數將隨便一個float類型的小數轉換成Decimal的數據類型,結果float類型數據就顯出原形了。

#?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('浮點數轉為Decimal后:{0}'.format(decimal_))

#?浮點數轉為Decimal后:10.2449999999999992184029906638897955417633056640625

從結果來看,float浮點數轉換完成以后精度位數就變得很長不是原先的三位小數了,這是因為float浮點數本身就不精確轉換之后才會出現上面的效果。

2.?Decimal除法設置

隨機選兩個整數將其定義為Decimal類型的數據,之后對這兩個整個做除法通過保留相應的結果位數對其返回結果進行優化。

#?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位小數后的結果是:{0}'.format(decimal_))

#?精確到8位小數后的結果是:0.16666667

很明顯做除法以后的結果應該是一個無限小數,設置保留8位小數之后自動進行了四舍五入的計算得到0.16666667的結果。

3. Quantize設置結果

同樣是保留了兩位小數,使用quantize函數能完成同樣的效果,默認結果也是經過了四舍五入的計算,若是想要固定小數位數使用此方法比較靠譜。

#?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設置保留兩位小數后的結果:{0}'.format(decimal_))

# quantize設置保留兩位小數后的結果:3.78

4.?Decimal精度設置

這里還是做一個結果為無限小數的除法,分別使用向上取整、向下取整的方式保留一定位數的小數來說明問題。

一般情況下可能使用的都是向上取整,但是在一些領域比較金融、證券行業就必須采取向下取整的方式,首先來看一下常用的向上取整的方式來保留小數。

#?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位小數:{0}'.format(decimal_.quantize(Decimal('0.0000000000'))))

#?向上取整保留10位小數:0.1111111112

這里有個問題就是,如果getcontext().prec已經設置小數位是10,那么在使用quantize函數固定小數位的時候就必須不超過10位才行,也就是不能超過有效位數否則就會報錯。

接下來看一下向下取整,向下取整的小數保留方式只需要修改getcontext().rounding的屬性為向下取整即可,為了對比結果我們還是采用同樣的數據來看看效果。

#?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位小數:{0}'.format(decimal_.quantize(Decimal('0.0000000000'))))

#?向下取整保留10位小數:0.1111111111

可以發現同樣的數據做除法,向下取整時結果是0.1111111111,向上取整時結果是0.1111111112。

同樣,還是很多的其他保留小數的方式可以使用比如四舍五入的方式,這也是很多很多行業會采取的一種小數保留的方式,再演示一下四舍五入時的保留小數。

#?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位小數:{0}'.format(decimal_.quantize(Decimal('0.0000'))))

#?四舍五入保留4位小數:3.1416

將3.14159通過四舍五入的方式保留4位小數之后就變成了3.1416,和我們預想的結果一樣。

原文鏈接:https://mp.weixin.qq.com/s/P1wapmw96-aFG1iULR9VlA

欄目分類
最近更新