English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Python, there is a module named Decimal, which is used to perform some tasks related to decimal floating-point numbers. This module provides correct rounding floating-point operations.
Firstly, we need to import the Decimal standard library module to use it.
import decimal
In this section, we will see some important functions of the Decimal module.
sqrt()
and exponential functionexp()
Dassqrt()
method is used to calculate the square root of a given decimal type object. And theexp()
The method returns the e^x value of the given x as a decimal number.
#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())
Ausgabewert
25.3599999999999994315658113919198513031005859375 Square Root is: 5.035871324805668565859161094 e^x is: 103206740212.7314661465187086
There are some logarithmic functions in the decimal module. Here, we discuss two of them. The first isln()
method. This method is used to find the natural logarithm of a decimal number.
Another method is log10() method. This method is used to find the logarithmic value with base10The logarithmic value with base e.
#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) ist: ') + str(my_dec.ln())) #Finde logarithmischen Wert mit Basis 10 print('log(x) ist: ') + str(my_dec.log10))
Ausgabewert
25.3599999999999994315658113919198513031005859375 ln(x) ist: 3.233173129569025152000878282 log(x) ist: 1.404149249209695070459909761
fma()
Methodeas_tuple-Methode wird verwendet, um den Dezimalwert in eine mitdreiElementen des Tuples. Die Elemente sindZeichen, ZiffernundExponentwert. Im Symbolfeld wird, wenn die Zahl 0 ist, der Dezimalwertpositiv;wenn die Zahl1dargestelltbei negativen Zahlen。
Dasfma()
Methode wirdFused Multiply and Add. Wenn wir fma(x, y) verwenden, berechnet es (number * x)+ y. In diesem Fall, (number * x) Teil wird nicht gerundet.
#Führe as_tuple() und fma() Methoden aus import decimal my_dec1 = decimal.Decimal(5.3) print(my_dec1) my_dec2 = decimal.Decimal(-9.23) print(my_dec2) #Zeige Dezimalzahl als Tupel print('\nmy_dec1 als Tupel: ' + str(my_dec1.as_tuple())) print('\nmy_dec2 als Tupel: ' + str(my_dec2.as_tuple())) #Führe Fused Multiply and Add aus print('\n(x*5)+8 ist: ' + str(my_dec1.fma(5, 8)))
Ausgabewert
5.29999999999999982236431605997495353221893310546875 -9.230000000000000426325641456060111522674560546875 my_dec1 als Tupel: 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 als Tupel: 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 ist: 34.49999999999999911182158030
compare()
MethodeDiese Vergleichsmethode wird verwendet, um zwei Dezimalzahlen zu vergleichen. Gibt es zurück 0, wenn die Zahlen gleich sind, sonst, wenn die erste Zahl größer ist, gibt es zurück+1Wenn der erste Parameter kleiner ist, gibt es zurück-1。
#Führe die compare() Methode aus import decimal #Vergleiche, wenn beide gleich sind print('Vergleiche Wert: ') + -5.3).compare(decimal.Decimal(',')),-5.3)))) #Vergleiche, wenn der erste Wert kleiner ist print('Vergleiche Wert: ') + -5.3).compare(decimal.Decimal(',')),9.26)))) #Vergleiche, wenn der erste Wert größer ist print('Vergleiche Wert: ') + -5.3).compare(decimal.Decimal(',')),-13.25))))
Ausgabewert
Vergleiche Wert: 0 Vergleiche Wert: -1 Vergleiche Wert: 1
Es gibt verschiedene Methoden, um einen Dezimalwert in ein anderes Dezimalobjekt zu kopieren. Das erste Verfahren ist copy_abs(). Es wird verwendet, um den Absolutwert eines Dezimalzahls zu erhalten. Das zweite Verfahren ist copy_negate(), das verwendet wird, um den Dezimalwert nach der Negation des tatsächlichen Wertes zu kopieren. Die dritte Funktion ist copy_sign(). Diese Methode gibt den ersten Parameter aus, indem sie das Zeichen vom zweiten Parameter holt.
#Führe copy_abs(), copy_negate() und copy_sign() aus import decimal my_dec = decimal.Decimal(-25.36) print(my_dec) #Kopiere den absoluten Wert temp_dec = my_dec.copy_abs() print('Absolut ist: ' + str(temp_dec)) #Kopiere den negativen Wert my_dec = decimal.Decimal(7.26) temp_dec = my_dec.copy_negate() print('Negation von 7.26 ist: ' + str(temp_dec)) #Kopiere den Zeichensatzwert vom zweiten Argument zum ersten my_dec = decimal.Decimal(-25.36) temp_dec = my_dec.copy_sign(decimal.Decimal(12.5)) print('Kopiere den Zeichensatz vom zweiten Argument: ' + str(temp_dec))
Ausgabewert
-25.3599999999999994315658113919198513031005859375 Absolut ist: 25.3599999999999994315658113919198513031005859375 Negation von 7.26 ist: -7.2599999999999997868371792719699442386627197265625 Kopiere den Zeichensatz vom zweiten Argument: 25.3599999999999994315658113919198513031005859375
Maximal- und Minimalwerte sind zwei einfache Methoden. Sie werden verwendet, um den größten oder kleinsten Wert zwischen zwei Zahlen zu finden.
#Führe max() und min() Methoden aus import decimal my_dec1 = decimal.Decimal(5.3) print(my_dec1) my_dec2 = decimal.Decimal(-9.23) print(my_dec2) #Zeige Minimum und Maximum print('Minumum: ' + str(my_dec1.min(my_dec2))) print('Maximum: ' + str(my_dec2.max(my_dec1)))
Ausgabewert
5.29999999999999982236431605997495353221893310546875 -9.230000000000000426325641456060111522674560546875 Minumum: -9.230000000000000426325641456 Maximum: 5.299999999999999822364316060