Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python decimal module – 7 functions you need to know!

PYTHON DECIMAL MODULE

Hey, all! In this article, we will have a look at one of the interesting modules – The Python Decimal module.

Be it any domain, we do come across the need to search for functions to perform mathematical operations. Python decimal module serves us with all the mathematical functions we need.

So, let us get started!


Understanding the Python decimal module

Python decimal module contains various functions to handle numeric data and perform different mathematical operations on it. Using the decimal module, we can handle decimal numbers efficiently throughout our program.

The decimal module provides us with functions to control and overcome the problem of precision in the decimal values.

Having understood the need of Decimal module, let us now have a look at some of the important functions offered by the module.

In order to use the functions, we need to import the module as shown below:

import decimal

The Decimal Module Functions and Implementation

Different arithmetic operations can be performed on the decimal or numeric data to boost the outcome.

We can define the decimal point numbers using the decimal.Decimal() function as shown below–

Syntax:

import decimal
variable = decimal.Decimal(decimal-number)

Further, we can control the precision value of the results of the decimal point numbers using an in-built function by the decimal module — decimal.getcontext().prec function.

Syntax:

decimal.getcontext().prec = precision value

The below-explained functions help us perform decimal point arithmetic operations in an efficient manner and at a great pace.


1. The exp() function – Exponent calculation

The exp() function computes the exponent value i.e. e^x of the particular decimal point number passed to it.

Syntax:

decimal.Decimal(decimal-number).exp()

Example:

import decimal as d

d.getcontext().prec = 5

#Intializing with an addition operation
val = d.Decimal(12.201) + d.Decimal(12.20)

#Calculating exponential of the decimal value
exp = val.exp()

#variable with no calculations
no_math = d.Decimal(1.131231)

print("Sum: ",val)
print("Exponential: ", exp)
print(no_math)

Output:

Decimal Number:  24.401                                                                                                       
3.9557E+10                                                                                                                    
1.131231000000000097571728474576957523822784423828125   

Notice how the total number of digits in our output are 5? That’s because of the precision value that we set up here.

One thing to remember is that the precision value applies when you perform mathematical operations on two decimals not when you directly initiate a variable with the values as shown with the “no_math” variable above.


2. The sqrt() function – Square Root

The sqrt() function calculates and returns the square root value of the passed decimal number to it.

Syntax:

decimal.Decimal(decimal-number).sqrt()

Example:

import decimal as d

d.getcontext().prec = 3

val = d.Decimal(122.20)
sqrt = val.sqrt()
print("Decimal Number: ",val)
print("Square root of the decimal number: ",sqrt)

Output:

Decimal Number:  122.2000000000000028421709430404007434844970703125
Square root of the decimal number:  11.1

Again, note how the declared value contains the complete decimal number while the computed value follows our precision set of 3 digits.

To find more mathematical operations, read our article on math module in Python


3. Logarithmic functions

Decimal module provides us with the below functions to calculate the logarithmic values of the decimal point numbers–

  • decimal.ln()
  • decimal.log10()

The decimal.ln() function returns the natural log value of the decimal number as shown below–

decimal.Decimal(decimal-number).ln()

The decimal.log10() function is used to calculate the log value of the base 10 of the decimal number passed to it.

decimal.Decimal(decimal-number).log10()

Example:

import decimal as d

d.getcontext().prec = 2

val = d.Decimal(122.20)

log = val.ln()
print("Natural log value of the decimal number: ",log)

log_10 = val.log10()
print("Log value with base 10 of the decimal number: ",log_10)

Output:

Natural log value of the decimal number:  4.8
Log value with base 10 of the decimal number:  2.1

4. The compare() function

The decimal.compare() function compares two decimal point numbers and returns the values depending upon the conditions as follows–

  • It returns -1, if the first decimal number is smaller than the second decimal number.
  • It returns 1, if the first decimal number is greater than the second decimal number.
  • Returns 0, if both the decimal point values are equal.

Example:

import decimal as d

valx = d.Decimal(122.20)
valy = d.Decimal(123.01)

print("Value 1: ",valx)
print("Value 2: ",valy)

compare = valx.compare(valy)
print(compare)

Output:

Value 1:  122.2000000000000028421709430404007434844970703125
Value 2:  123.0100000000000051159076974727213382720947265625
-1

5. The copy_abs() function

The decimal.copy_abs() function returns the absolute values of the signed decimal number passed to it.

Syntax:

decimal.Decimal(signed decimal number).copy_abs()

Example:

import decimal as d

valx = d.Decimal(-122.20)
print("Value 1: ",valx)

absolute = valx.copy_abs()
print("Absolute value of the given decimal number: ",absolute)

Output:

Value 1:  -122.2000000000000028421709430404007434844970703125
Absolute value of the given decimal number:  122.2000000000000028421709430404007434844970703125

6. Max and Min functions

The Python decimal module contains the following functions to calculate the minimum and maximum values of the decimal point numbers.

  • min() function: Returns the minimum of the two decimal values.
  • max() function: Returns the maximum of the two decimal values.
#Syntax for min() function-
decimal1.min(decimal2)

#Syntax for max() function-
decimal1.max(decimal2)

Example:

import decimal as d

valx = d.Decimal(122.20)
valy = d.Decimal(123.01)

print("Value 1: ",valx)
print("Value 2: ",valy)

min_val = valx.min(valy)
print("The minimum of the two values: ",min_val)

max_val = valx.max(valy)
print("The maximum of the two values: ",max_val)


Output:

Value 1:  122.2000000000000028421709430404007434844970703125
Value 2:  123.0100000000000051159076974727213382720947265625
The minimum of the two values:  122.2000000000000028421709430
The maximum of the two values:  123.0100000000000051159076975

7. Logical operations with decimal module

Decimal module contains a set of in-built functions to perform logical operations on the decimal numbers such as AND, OR, XOR, etc.

  • logical_and() function: It performs logical AND operation on the two decimal numbers and returns the result.
  • logical_or() function: It performs logical OR operation on the two decimal numbers and returns the result.
  • logical_xor() function: It performs logical XOR operation on the two decimal numbers and returns the result.
#Syntax for logical_and() function-
decimal1.logical_and(decimal2)

#Syntax for logical_or() function-
decimal1.logical_or(decimal2)

#Syntax for logical_xor() function-
decimal1.logical_xor(decimal2)

Example:

import decimal as d

valx = d.Decimal(1001)
valy = d.Decimal(1111)

print("Value 1: ",valx)
print("Value 2: ",valy)

AND = valx.logical_and(valy)
print("The logical AND value of the two decimals: ",AND)

OR = valx.logical_or(valy)
print("The logical OR value of the two decimals: ",OR)

XOR = valx.logical_xor(valy)
print("The logical XOR value of the two decimals: ",XOR)


Output:

Value 1:  1001
Value 2:  1111
The logical AND value of the two decimals:  1001
The logical OR value of the two decimals:  1111
The logical XOR value of the two decimals:  110

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

Till then, Happy Learning!!


References