Decimal Functions in Python

PythonProgrammingServer Side Programming

In Python, there is a module called Decimal, which is used to do some decimal floating point related tasks. This module provides correctly-rounded floating point arithmetic.

To use it at first we need to import it the Decimal standard library module.

import decimal


In this section we will see some important functions of the Decimal module.

The square root function sqrt() and Exponent function exp()

The sqrt() method is used to calculate the square root of a given decimal type object. And the exp() method returns the e^x value for the given x as Decimal number.

Example Code

#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

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


The logarithmic functions

There are some logarithmic functions in the Decimal module. Here we are discussing about two of them. The first one is the ln() method. This method is used to find the natural logarithm of the decimal number.

Another method is log10() method. This method is used to find the logarithmic value where base is 10.

Example Code

#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

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


The as_tuple() and the fma() method

The as_tuple method is used to represent the decimal as a tuple with three elements. The elements are sign, digits and the exponent value. In the sign field when the number is 0, it means the decimal is positive, when it is 1, it represents the negative number.

The fma() method is known as the fused multiplication and add. If we use fma(x, y) It will compute the (number * x) + y. In this case the (number*x) part is not rounded off.

Example Code

#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()))
print('\n(x*5)+8 is: ' + str(my_dec1.fma(5, 8)))


Output

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


The compare() method

This compare method is for comparing two decimal numbers. When the numbers are same, it will return 0, otherwise, when the first number is greater, it will give +1, and when first argument is smaller, it will return -1.

Example Code

#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))))


Output

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


Some copying functions

There are some different methods for copying decimal numbers into another decimal object. The first method is copy_abs(). It is used to get the absolute value from the decimal number. The second method is copy_negate(), It 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 taking the sign from the second argument.

Example Code

#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

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


The max and min methods

The max and min are two simple methods. These are used to find the maximum or minimum between two numbers respectively.

Example Code

#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)))


Output

5.29999999999999982236431605997495353221893310546875
-9.230000000000000426325641456060111522674560546875
Minumum: -9.230000000000000426325641456
Maximum: 5.299999999999999822364316060

Updated on 30-Jul-2019 22:30:23