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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Python+decimal完成精度計(jì)算的示例詳解_python

作者:Sir?老王 ? 更新時(shí)間: 2022-11-27 編程語(yǔ)言

在進(jìn)行小數(shù)計(jì)算的時(shí)候使用float,經(jīng)常會(huì)出現(xiàn)小數(shù)位不精確的情況。在python編程中,推薦使用decimal來(lái)完成小數(shù)位的精度計(jì)算。

decimal是python中的標(biāo)準(zhǔn)庫(kù),直接將Decimal導(dǎo)入到代碼塊中使用。

decimal意思為十進(jìn)制,這個(gè)模塊提供了十進(jìn)制浮點(diǎn)運(yùn)算支持。通過(guò)幾個(gè)常見(jiàn)的實(shí)戰(zhàn)用例來(lái)說(shuō)明一下其用法。

1.?浮點(diǎn)數(shù)轉(zhuǎn)Decimal

使用Decimal.from_float函數(shù)將隨便一個(gè)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('浮點(diǎn)數(shù)轉(zhuǎn)為Decimal后:{0}'.format(decimal_))

#?浮點(diǎn)數(shù)轉(zhuǎn)為Decimal后:10.2449999999999992184029906638897955417633056640625

從結(jié)果來(lái)看,float浮點(diǎn)數(shù)轉(zhuǎn)換完成以后精度位數(shù)就變得很長(zhǎng)不是原先的三位小數(shù)了,這是因?yàn)閒loat浮點(diǎn)數(shù)本身就不精確轉(zhuǎn)換之后才會(huì)出現(xiàn)上面的效果。

2.?Decimal除法設(shè)置

隨機(jī)選兩個(gè)整數(shù)將其定義為Decimal類型的數(shù)據(jù),之后對(duì)這兩個(gè)整個(gè)做除法通過(guò)保留相應(yīng)的結(jié)果位數(shù)對(duì)其返回結(jié)果進(jìn)行優(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é)果應(yīng)該是一個(gè)無(wú)限小數(shù),設(shè)置保留8位小數(shù)之后自動(dòng)進(jìn)行了四舍五入的計(jì)算得到0.16666667的結(jié)果。

3. Quantize設(shè)置結(jié)果

同樣是保留了兩位小數(shù),使用quantize函數(shù)能完成同樣的效果,默認(rèn)結(jié)果也是經(jīng)過(guò)了四舍五入的計(jì)算,若是想要固定小數(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è)置

這里還是做一個(gè)結(jié)果為無(wú)限小數(shù)的除法,分別使用向上取整、向下取整的方式保留一定位數(shù)的小數(shù)來(lái)說(shuō)明問(wèn)題。

一般情況下可能使用的都是向上取整,但是在一些領(lǐng)域比較金融、證券行業(yè)就必須采取向下取整的方式,首先來(lái)看一下常用的向上取整的方式來(lái)保留小數(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

這里有個(gè)問(wèn)題就是,如果getcontext().prec已經(jīng)設(shè)置小數(shù)位是10,那么在使用quantize函數(shù)固定小數(shù)位的時(shí)候就必須不超過(guò)10位才行,也就是不能超過(guò)有效位數(shù)否則就會(huì)報(bào)錯(cuò)。

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

#?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ù)做除法,向下取整時(shí)結(jié)果是0.1111111111,向上取整時(shí)結(jié)果是0.1111111112。

同樣,還是很多的其他保留小數(shù)的方式可以使用比如四舍五入的方式,這也是很多很多行業(yè)會(huì)采取的一種小數(shù)保留的方式,再演示一下四舍五入時(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通過(guò)四舍五入的方式保留4位小數(shù)之后就變成了3.1416,和我們預(yù)想的結(jié)果一樣。

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

欄目分類
最近更新