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 of floating-point operations.
Firstly, to use it, we need to import the Decimal standard library module.
import decimal
In this section, we will see some important features of the Decimal module.
sqrt()
and exponential functionexp()
thesqrt()
method is used to calculate the square root of a given decimal type object. And theexp()
This method returns the value of e^x for 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())
Output Result
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) is: ' + str(my_dec.ln())) #Find logarithmic value with base 10 print('log(x) is: ' + str(my_dec.log10))
Output Result
25.3599999999999994315658113919198513031005859375 ln(x) is: 3.233173129569025152000878282 log(x) is: 1.404149249209695070459909761
fma()
methodas_tuple method is used to represent the decimal as havingthreetuple ofsign, digits, theandexponentvalue. In the sign field, when the number is 0, it represents the decimal aspositive; when the number is1representwhen negative numbers.
thefma()
method calledFused Multiply and Add. If we use fma(x, y), it will calculate (number * x)+ y. In this case, (number * x part will not be rounded.
#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))
Output Result
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()
methodThis comparison method is used to compare two decimal numbers. When the numbers are the same, it will return 0, otherwise, when the first number is larger, it will give+1,when the first parameter is smaller, it will return-1.
#Perform compare() method import decimal #Compare when both are equal + -5.3).compare(decimal.Decimal(-5.3)))) #Compare when the first one is smaller + -5.3).compare(decimal.Decimal(9.26)))) #Compare when the first one is greater + -5.3).compare(decimal.Decimal(-13.25))))
Output Result
Compare value: 0 Compare value: -1 Compare value: 1
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.
#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))
Output Result
-25.3599999999999994315658113919198513031005859375 Absolute is: 25.3599999999999994315658113919198513031005859375 Negate of 7.26 is: -7.2599999999999997868371792719699442386627197265625 Copy sign from second argument: 25.3599999999999994315658113919198513031005859375
Max and min are two simple methods. They are used to find the maximum or minimum value between two numbers respectively.
#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('Minimum: ' + str(my_dec1.min(my_dec2)) print('Maximum: ' + str(my_dec2.max(my_dec1))
Output Result
5.29999999999999982236431605997495353221893310546875 -9.230000000000000426325641456060111522674560546875 Minimum: -9.230000000000000426325641456 Maximum: 5.299999999999999822364316060