Computer >> Computer tutorials >  >> Programming >> Python

What is Decimal Functions in Python?


For decimal floating-point arithmetic python provides the decimal module. This module itself has hundreds of functions that help in the efficient processing of decimal calculations. We will look at the important and most widely used ones on this topic.

compare()

This function compares decimal numbers. Returns 1 if 1st Decimal argument is greater than 2nd, -1 if 1st Decimal argument is smaller than 2nd and 0 if both are equal.

Example

import decimal

val1 = decimal.Decimal(2.6)
val2 = decimal.Decimal(2.61)

# compare decimals
print("The result is : ",val1.compare(val2))

# resetting the values
val1 = decimal.Decimal(2.6)
val2 = decimal.Decimal(-2.6)

# compare decimals
print("The result is : ",val1.compare(val2))

# resetting the values
val1 = decimal.Decimal(2.6)
val2 = decimal.Decimal(2.6)

# compare decimals
print("The result is : ",val1.compare(val2))

Output

Running the above code gives us the following result −

The result is : -1
The result is : 1
The result is : 0

max() and min()

They find the maximum and minimum of two decimal numbers respectively.

Example

import decimal

val1 = decimal.Decimal(2.6)
val2 = decimal.Decimal(2.61)

# compare decimals
print("The max value is : ",round(val1.max(val2),2))
print("The min value is : ",round(val1.min(val2),2))

Output

Running the above code gives us the following result −

The max value is : 2.61
The min value is : 2.60

getcontext()

We can change the precision of arithmetic operations by using this method. The default precision is 28. In the below example we carry out an arithmetic operation which shows the result as per the precision set by getcontext().prec.

Example

from decimal import *
print(Decimal(13) / Decimal(7))

getcontext().prec = 6
print(Decimal(13) / Decimal(7))

getcontext().prec = 10
print(Decimal(13) / Decimal(7))

Output

Running the above code gives us the following result −

1.857142857142857142857142857
1.85714
1.857142857

exp()

Return the value of the (natural) exponential function e**x at the given number.

Example

from decimal import *

#Finding e
print(Decimal(1).exp())

#Finding e raised to 2
print(Decimal(2).exp())

#Finding e raised to 4
print(Decimal(4).exp())

Output

Running the above code gives us the following result −

2.718281828459045235360287471
7.389056098930650227230427461
54.59815003314423907811026120

as_integer_ratio()

Sometimes we need the integers whose division gives us the decimal we are dealing with. This we can obtain by using the as_integer_ratio ().

Example

from decimal import *

v = Decimal('2.1834').as_integer_ratio()
print(v)

v = Decimal('-1.92').as_integer_ratio()
print(v)

Output

Running the above code gives us the following result −

(10917, 5000)
(-48, 25)

ln() and log10()

We can calculate the natural logarithm ( with a base as e) as well as the base 10 logarithm using these functions. We supply the decimal value whose logarithms are required.

Example

from decimal import *

ln_val = Decimal('2.1').ln()
print(ln_val)

log_val = Decimal('2.1').log10()
print(log_val)

Output

Running the above code gives us the following result −

0.7419373447293773124826065257
0.3222192947339192680072441618

fma(a,b)

This is a special function that is called fused multiply and add. The supplied decimal gets multiplied with the first argument a and then the result gets added to the second argument b.

Example

from decimal import *

# Same as (2.1*2)+5
fma_val = Decimal(2.1).fma(2,5)
print(fma_val)

# Same as (8.1*3)+5
fma_val = Decimal(8.1).fma(3,5)

print(fma_val)
re class="prettyprint notranslate" >

Output

Running the above code gives us the following result −

9.200000000000000177635683940
29.29999999999999893418589636