English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Funzione decimale in Python

In Python, there is a module called Decimal, which is used to perform some tasks related to decimal floating-point numbers. This module provides floating-point arithmetic with correct rounding.

首先要使用它,我们需要将其导入Decimal标准库模块。

import decimal

在本节中,我们将看到Decimal模块的一些重要功能。

平方根函数sqrt()和指数函数exp()

Questosqrt()方法用于计算给定十进制类型对象的平方根。并且该exp()方法返回给定x的e ^ x值作为十进制数。

Codice di esempio

#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 è: ' + str(my_dec.sqrt()))
#Find e^x for the decimal number
print('e^x è: ' + str(my_dec.exp()))

Risultato di output

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

对数函数

小数模块中有一些对数函数。在这里,我们讨论其中的两个。第一个是ln()方法。此方法用于查找十进制数字的自然对数。

另一个方法是log10()方法。此方法用于查找以10为底的对数值。

Codice di esempio

#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) è: ' + str(my_dec.ln()))
#Find logarithmic value with base 10
print('log(x) è: ' + str(my_dec.log10()))

Risultato di output

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

as_tuple()和fma()Metodo

as_tuple方法用于将十进制表示为具有三个元素的元组。元素是符号,数字指数值。在符号字段中,当数字为0时,表示十进制为positivo; Quando il numero è 1, rappresentaNumero negativo.

Questofma()Il metodo chiamatoMoltiplicazione e Somma Fusa. Se utilizziamo fma(x,y), calcolerà (number * x) + y. In questo caso, la parte (number * x) non viene arrotondata.

Codice di esempio

#Esegui i metodi as_tuple() e fma()
import decimal
my_dec1 = decimal.Decimal(5.3)
print(my_dec1)
my_dec2 = decimal.Decimal(-9.23)
print(my_dec2)
#Mostra il numero decimale come tupla
print('\nmy_dec1 come tupla: ' + str(my_dec1.as_tuple()))
print('\nmy_dec2 come tupla: ' + str(my_dec2.as_tuple()))
#Esegui il metodo Moltiplicazione e Somma Fusa
print('\n(x*5)+8 è: ' + str(my_dec1.fma(5, 8)))

Risultato di output

5.29999999999999982236431605997495353221893310546875
-9.230000000000000426325641456060111522674560546875
my_dec1 come tupla: DecimalTuple(sign=0, cifre=(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), esponente=-50)
my_dec2 come tupla: DecimalTuple(sign=1, cifre=(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), esponente=-48)
(x*5)+8 è: 34.49999999999999911182158030

Questocompare()Metodo

Questo metodo di confronto viene utilizzato per confrontare due numeri decimali. Quando i numeri sono uguali, restituisce 0, altrimenti, quando il primo numero è maggiore, restituisce +1, mentre se il primo parametro è minore, restituisce -1.

Codice di esempio

#Esegui il metodo compare()
import decimal
#Confronta quando entrambi sono uguali
print('Compare value: ' + str(decimal.Decimal(-5.3).compare(decimal.Decimal(-5.3))))
#Compare when the first one is smaller
print('Compare value: ' + str(decimal.Decimal(-5.3).compare(decimal.Decimal(9.26))))
#Compare when the first one is greater
print('Compare value: ' + str(decimal.Decimal(-5.3).compare(decimal.Decimal(-13.25))))

Risultato di output

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

Some copy functions

There are several different methods to copy a decimal number to another decimal object. The first method is copy_abs(). It is used to get the absolute value from a decimal number. The second method is copy_negate(), which is used to copy the decimal number after negating the actual number. The third function is copy_sign(). This method prints the first argument by getting the sign from the second parameter.

Codice di esempio

#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('Copia del segno dal secondo argomento: ' + str(temp_dec))

Risultato di output

-25.3599999999999994315658113919198513031005859375
Valore assoluto è: 25.3599999999999994315658113919198513031005859375
Negativo di 7.26 è: -7.2599999999999997868371792719699442386627197265625
Copia del segno dal secondo argomento: 25.3599999999999994315658113919198513031005859375

Metodi massimo e minimo

Massimo e minimo sono due metodi semplici. Servono rispettivamente a trovare il valore massimo o minimo tra due numeri.

Codice di esempio

#Eseguire i metodi max() e min()
import decimal
my_dec1 = decimal.Decimal(5.3)
print(my_dec1)
my_dec2 = decimal.Decimal(-9.23)
print(my_dec2)
#Mostra Minimo e Massimo
print('Minimo: ' + str(my_dec1.min(my_dec2)))
print('Massimo: ' + str(my_dec2.max(my_dec1)))

Risultato di output

5.29999999999999982236431605997495353221893310546875
-9.230000000000000426325641456060111522674560546875
Minimo: -9.230000000000000426325641456
Massimo: 5.299999999999999822364316060