Python中的十進位制函式


在Python中,有一個名為Decimal的模組,用於執行一些與十進位制浮點數相關的任務。此模組提供正確舍入的浮點運算。

首先,我們需要匯入Decimal標準庫模組才能使用它。

import decimal

在本節中,我們將瞭解Decimal模組的一些重要函式。

平方根函式sqrt()和指數函式exp()

sqrt()方法用於計算給定十進位制型別物件的平方根。exp()方法返回給定x(作為十進位制數)的e^x值。

示例程式碼

#Perform sqrt() and exp() methods
import decimal
my_dec = decimal.Decimal(25.36)
print(my_dec)
#Find Square Root of the decimal number
print('Square Root is: ' + str(my_dec.sqrt()))
#Find e^x for the decimal number
print('e^x is: ' + str(my_dec.exp()))

輸出

25.3599999999999994315658113919198513031005859375
Square Root is: 5.035871324805668565859161094
e^x is: 103206740212.7314661465187086

對數函式

Decimal模組中有一些對數函式。這裡我們討論其中的兩個。第一個是ln()方法。此方法用於查詢十進位制數的自然對數。

另一個方法是log10()方法。此方法用於查詢以10為底的對數值。

示例程式碼

#Perform ln() and log10() methods
import decimal
my_dec = decimal.Decimal(25.36)
print(my_dec)
#Find logarithmic value with base e
print('ln(x) is: ' + str(my_dec.ln()))
#Find logarithmic value with base 10
print('log(x) is: ' + str(my_dec.log10()))

輸出

25.3599999999999994315658113919198513031005859375
ln(x) is: 3.233173129569025152000878282
log(x) is: 1.404149249209695070459909761

as_tuple()和fma()方法

as_tuple方法用於將十進位制數表示為包含三個元素的元組。這些元素分別是符號、數字指數值。在符號欄位中,當數字為0時,表示十進位制數為正數;當數字為1時,表示負數

fma()方法稱為融合乘加。如果我們使用fma(x, y),它將計算(number * x) + y。在這種情況下,(number * x)部分不會被舍入。

示例程式碼

#Perform as_tuple() and fma() methods
import decimal
my_dec1 = decimal.Decimal(5.3)
print(my_dec1)
my_dec2 = decimal.Decimal(-9.23)
print(my_dec2)
#Show decimal as tuple
print('\nmy_dec1 as tuple: ' + str(my_dec1.as_tuple()))
print('\nmy_dec2 as tuple: ' + str(my_dec2.as_tuple()))
#Perform Fused Multiply and Add
print('\n(x*5)+8 is: ' + str(my_dec1.fma(5, 8)))

輸出

5.29999999999999982236431605997495353221893310546875
-9.230000000000000426325641456060111522674560546875

my_dec1 as tuple: DecimalTuple(sign=0, digits=(5, 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 2, 2, 3, 6, 4, 
3, 1, 6, 0, 5, 9, 9, 7, 4, 9, 5, 3, 5, 3, 2, 2, 1, 8, 9, 3, 3, 1, 0, 5, 4, 6, 8, 7, 5), exponent=-50)

my_dec2 as tuple: DecimalTuple(sign=1, digits=(9, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 6, 3, 2, 5,
 6, 4, 1, 4, 5, 6, 0, 6, 0, 1, 1, 1, 5, 2, 2, 6, 7, 4, 5, 6, 0, 5, 4, 6, 8, 7, 5), exponent=-48)

(x*5)+8 is: 34.49999999999999911182158030

compare()方法

此compare方法用於比較兩個十進位制數。當數字相同時,它將返回0;否則,當第一個數字較大時,它將返回+1;當第一個引數較小時,它將返回-1。

示例程式碼

#Perform compare() method
import decimal
#Compare when both are equal
print('Compare value: ' + str(decimal.Decimal(-5.3).compare(decimal.Decimal(-5.3))))
#Compare when first one is smaller
print('Compare value: ' + str(decimal.Decimal(-5.3).compare(decimal.Decimal(9.26))))
#Compare when first one is greater
print('Compare value: ' + str(decimal.Decimal(-5.3).compare(decimal.Decimal(-13.25))))

輸出

Compare value: 0
Compare value: -1
Compare value: 1

一些複製函式

有一些不同的方法可以將十進位制數複製到另一個十進位制物件中。第一種方法是copy_abs()。它用於從十進位制數中獲取絕對值。第二種方法是copy_negate(),它用於在對實際數字取反後複製十進位制數。第三個函式是copy_sign()。此方法透過從第二個引數獲取符號來列印第一個引數。

示例程式碼

#Perform copy_abs(), copy_negate() and copy_sign()
import decimal
my_dec = decimal.Decimal(-25.36)
print(my_dec)
#copy the absolute value
temp_dec = my_dec.copy_abs()
print('Absolute is: ' + str(temp_dec))
#copy the negative value
my_dec = decimal.Decimal(7.26)
temp_dec = my_dec.copy_negate()
print('Negate of 7.26 is: ' + str(temp_dec))
#copy the sign value from second argument to first one
my_dec = decimal.Decimal(-25.36)
temp_dec = my_dec.copy_sign(decimal.Decimal(12.5))
print('Copy sign from second argument: ' + str(temp_dec))

輸出

-25.3599999999999994315658113919198513031005859375
Absolute is: 25.3599999999999994315658113919198513031005859375
Negate of 7.26 is: -7.2599999999999997868371792719699442386627197265625
Copy sign from second argument: 25.3599999999999994315658113919198513031005859375

max和min方法

max和min是兩個簡單的函式。它們分別用於查詢兩個數字之間的最大值或最小值。

示例程式碼

#Perform max() and min() methods
import decimal
my_dec1 = decimal.Decimal(5.3)
print(my_dec1)
my_dec2 = decimal.Decimal(-9.23)
print(my_dec2)
#Show Minimum and Maximum
print('Minumum: ' + str(my_dec1.min(my_dec2)))
print('Maximum: ' + str(my_dec2.max(my_dec1)))

輸出

5.29999999999999982236431605997495353221893310546875
-9.230000000000000426325641456060111522674560546875
Minumum: -9.230000000000000426325641456
Maximum: 5.299999999999999822364316060

更新於:2019年7月30日

13K+ 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.