Python decimal module: We come across the necessity to seek functions to conduct mathematical operations in any domain. The Python decimal module provides us with all of the mathematical functions we require.
decimal Module in Python:
The Python decimal module includes a number of functions for dealing with numeric data and performing various mathematical operations on it. Using the decimal module, we can efficiently handle decimal numbers throughout our program.
The decimal module contains utilities for controlling and overcoming the precision problem in decimal values.
Before using the module, we should import it first.
import decimal
- How to Truncate a float in Python?
- Python Program to Convert Hexadecimal to Decimal
- Python Program to Print Binary Representation of a Number
Functions Present in decimal Module
To improve the outcome, various arithmetic operations can be done on the decimal or numeric data.
Using the decimal, we may define decimal point numbers.
As demonstrated below, the Decimal() function is:
Syntax:
import decimal variable = decimal.Decimal(decimal_number)
Use decimal.getcontext().prec function which is an in-built function provided by the decimal module to control the precision value of the results of the decimal point number.
Syntax:
decimal.getcontext().prec = precision value
1)exp() Function:
This function returns the exponent value (e^x) of the given decimal number.
Syntax:
decimal.Decimal(decimal_num).exp()
Example
# Import decimal module using the import keyword import decimal # Give the precision value as static input using the decimal.getcontext().prec # function and store it in a variable. decimal.getcontext().prec = 4 # Give the first decimal number as static input using the Decimal() function # and store it in a variable. fst_num = decimal.Decimal(15.53) # Give the second decimal number as static input and store it in another variable. scnd_num = decimal.Decimal(10.4) # Calculate the sum of the given two decimal numbers and # store it in another variable. rslt_sum = fst_num + scnd_num # Get the exponential value of the result sum obtained using the exp() function. # (e^rslt_sum) # Store it in a variable exp_val = rslt_sum.exp() # Pass some random decimal number to the Decimal() function(which stores number in # the form of 32-bit) and store it in another variable. nomath_samplnum = decimal.Decimal(5.132) # Print the Sum of the given two decimal numbers print("The Sum of the given two decimal numbers = ", rslt_sum) # Print the Exponential value of the result sum obtained print("The Exponential value of the result sum obtained = ", exp_val) # Print the above given no math sample number. print("The above given no math sample number = ") print(nomath_samplnum)
Output:
The Sum of the given two decimal numbers = 25.93 The Exponential value of the result sum obtained = 1.825E+11 The above given no math sample number = 5.1319999999999996731503415503539144992828369140625
Have you noticed that the total number of digits in our output is 4? This is due to the precision value that we specified here.
One thing to keep in mind is that the precision value only applies when performing mathematical operations on two decimals, not when immediately initiating a variable with the values provided above with the “nomath_samplnum ” variable.
2)sqrt() Function:
The sqrt() function computes and returns the square root value of the given decimal number.
Syntax:
decimal.Decimal(decimal_num).sqrt()
Example
# Import decimal module using the import keyword import decimal # Give the precision value as static input using the decimal.getcontext().prec # function and store it in a variable. decimal.getcontext().prec = 4 # Give the decimal number as static input using the Decimal() function # and store it in a variable. gvn_num = decimal.Decimal(25.54) # Get the square root value of the given decimal number using the sqrt() function. # Store it in another variable rslt_sqrtval = gvn_num.sqrt() # Print the given decimal number. print("The given decimal number = ", gvn_num) # Print the square root value of the given decimal number. print("The given decimal number's square root value = ", rslt_sqrtval)
Output:
The given decimal number = 25.53999999999999914734871708787977695465087890625 The given decimal number's square root value = 5.054
3)Logarithmic functions( ln(), log10() )
The Decimal module includes the following methods for calculating the logarithmic values of decimal point numbers–
- decimal.ln()
- decimal.log10()
The decimal.ln() function returns the natural log value of the given decimal number
Syntax:
decimal.Decimal(decimal_num).ln()
The decimal.log10() function computes the base 10 log value of the given decimal number.
Syntax:
decimal.Decimal(decimal_num).log10()
Example
# Import decimal module using the import keyword import decimal # Give the precision value as static input using the decimal.getcontext().prec # function and store it in a variable. decimal.getcontext().prec = 4 # Give the decimal number as static input using the Decimal() function # and store it in a variable. gvn_num = decimal.Decimal(25.54) # Calculate the natural Logarithmic value of the given decimal number # using the ln() function. # Store it in another variable log_val = gvn_num.ln() # Calculate the base 10 Logarithmic value of the given decimal number # using the log10() function. # Store it in another variable base10_logval = gvn_num.log10() # Print the given decimal number natural log value print("The given decimal number natural log value = ", log_val) # Print the given decimal number's base 10 log value print("The given decimal number's base 10 log value = ", base10_logval)
Output:
The given decimal number natural log value = 3.240 The given decimal number's base 10 log value = 1.407
4)compare() Function in Python
The decimal.compare() method compares two decimal point integers and returns values based on the following conditions–
- If the first decimal number is less than the second decimal number, it returns -1.
- If the first decimal number is bigger than the second decimal number, it returns 1.
- If the decimal point values are equal, this function returns 0.
Example
# Import decimal module using the import keyword import decimal # Give the precision value as static input using the decimal.getcontext().prec # function and store it in a variable. decimal.getcontext().prec = 4 # Give the first decimal number as static input using the Decimal() function # and store it in a variable. fst_num = decimal.Decimal(15.53) # Give the second decimal number as static input and store it in another variable. scnd_num = decimal.Decimal(10.4) # Compare both the given numbers using compare() function and # store it in another variable. comparisn_rslt = fst_num.compare(scnd_num) # Print the comparision result. print("The comparision result = ", comparisn_rslt)
Output:
The comparision result = 1
5)copy_abs() function:
The function decimal.copy_abs() returns the absolute values of the signed decimal number provided to it.
Syntax:
decimal.Decimal(signed decimal num).copy_abs()
Example
# Import decimal module using the import keyword import decimal # Give the decimal number as static input using the Decimal() function # and store it in a variable. gvn_num = decimal.Decimal(-16.345) # Get the absolute value of the given nunber using the copy_abs() function # and store it in another variable. abs_val = gvn_num.copy_abs() # Print the given decimal number's Absolute value. print("The given decimal number's Absolute value = ", abs_val)
Output:
The given decimal number's Absolute value = 16.344999999999998863131622783839702606201171875
6)Min and Max Functions
The Python decimal module includes the functions listed below for calculating the minimum and maximum values of decimal point numbers.
- The min() function returns the smallest/minimum of two decimal numbers.
- The max() function returns the greatest/maximum of the two decimal numbers.
min() Function Syntax:
decimalnum_1.min(decimalnum_2)
max() function Syntax:
decimalnum_1.max(decimalnum_2)
Example
# Import decimal module using the import keyword import decimal # Give the first decimal number as static input using the Decimal() function # and store it in a variable. fst_num = decimal.Decimal(15.53) # Give the second decimal number as static input and store it in another variable. scnd_num = decimal.Decimal(10.4) # Get the smallest value of the given two decimal numbers using the min() function # Store it in another variable. minimm_num = fst_num.min(scnd_num) # Print the smallest value of the given two decimal numbers print("The smallest value of the given two decimal numbers = ", minimm_num) # Get the greatest value of the given two decimal numbers using the max() function # Store it in another variable. maxim_num = fst_num.max(scnd_num) # Print the greatest value of the given two decimal numbers print("The greatest value of the given two decimal numbers = ", maxim_num)
Output:
The smallest value of the given two decimal numbers = 10.40000000000000035527136788 The greatest value of the given two decimal numbers = 15.52999999999999936051153782
7)Logical Operations
The Decimal module includes a set of built-in functions for performing logical operations on decimal integers such as AND, OR, XOR, and so on.
logical_and() function: This function performs the logical AND operation on two decimal numbers and returns the result.
logical_or():
The method logical_or() performs a logical OR operation on two decimal values and returns the result.
logical_xor():
The function logical_xor() performs a logical XOR operation on two decimal values and returns the result.
Syntax:
# logical_and() decimalnum_1.logical_and(decimalnum_2) # logical_or() decimalnum_1.logical_or(decimalnum_2) # logical_xor decimalnum_1.logical_xor(decimalnum_2)
Example:
# Import decimal module using the import keyword import decimal # Give the first decimal number as static input using the Decimal() function # and store it in a variable. fst_num = decimal.Decimal(1100) # Give the second decimal number as static input and store it in another variable. scnd_num = decimal.Decimal(1010) # Calculate the logical AND value of the given two numbers using the logical_and() # function and store it in another variable. logicl_AND = fst_num.logical_and(scnd_num) print("The given two decimal number's logical AND value = ", logicl_AND) # Calculate the logical OR value of the given two numbers using the logical_and() # function and store it in another variable. logicl_OR = fst_num.logical_or(scnd_num) print("The given two decimal number's logical OR value = ", logicl_OR) # Calculate the logical XOR value of the given two numbers using the logical_and() # function and store it in another variable. logicl_XOR = fst_num.logical_xor(scnd_num) print("The given two decimal number's logical XOR value = ", logicl_XOR)
Output:
The given two decimal number's logical AND value = 1000 The given two decimal number's logical OR value = 1110 The given two decimal number's logical XOR value = 110