Decimal Functions in Python

The Decimal module in Python provides precise decimal floating-point arithmetic, avoiding the common rounding errors that occur with standard floating-point numbers. This module is particularly useful for financial calculations where precision is critical.

To use the Decimal module, you need to import it first ?

import decimal

Square Root and Exponential Functions

The sqrt() method calculates the square root of a decimal number, while exp() returns ex for a given decimal value ?

import decimal

my_dec = decimal.Decimal(25.36)
print(my_dec)
print('Square Root is:', my_dec.sqrt())
print('e^x is:', my_dec.exp())
25.3599999999999994315658113919198513031005859375
Square Root is: 5.035871324805668565859161094
e^x is: 103206740212.7314661465187086

Logarithmic Functions

The Decimal module provides ln() for natural logarithm and log10() for base-10 logarithm ?

import decimal

my_dec = decimal.Decimal(25.36)
print(my_dec)
print('ln(x) is:', my_dec.ln())
print('log10(x) is:', my_dec.log10())
25.3599999999999994315658113919198513031005859375
ln(x) is: 3.233173129569025152000878282
log10(x) is: 1.404149249209695070459909761

Tuple Representation and Fused Multiply-Add

The as_tuple() method represents a decimal as a tuple with three elements: sign (0 for positive, 1 for negative), digits, and exponent. The fma() method performs fused multiplication and addition ?

import decimal

my_dec1 = decimal.Decimal(5.3)
my_dec2 = decimal.Decimal(-9.23)

print('my_dec1:', my_dec1)
print('my_dec2:', my_dec2)
print('\nmy_dec1 as tuple:', my_dec1.as_tuple())
print('\nmy_dec2 as tuple:', my_dec2.as_tuple())
print('\n(5.3*5)+8 is:', my_dec1.fma(5, 8))
my_dec1: 5.29999999999999982236431605997495353221893310546875
my_dec2: -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)

(5.3*5)+8 is: 34.49999999999999911182158030

Comparison Method

The compare() method compares two decimal numbers and returns 0 if equal, 1 if the first is greater, and -1 if the first is smaller ?

import decimal

# Compare when both are equal
result1 = decimal.Decimal(-5.3).compare(decimal.Decimal(-5.3))
print('Equal comparison:', result1)

# Compare when first one is smaller
result2 = decimal.Decimal(-5.3).compare(decimal.Decimal(9.26))
print('First smaller:', result2)

# Compare when first one is greater
result3 = decimal.Decimal(-5.3).compare(decimal.Decimal(-13.25))
print('First greater:', result3)
Equal comparison: 0
First smaller: -1
First greater: 1

Copy Methods

The Decimal module provides three copy methods: copy_abs() for absolute value, copy_negate() for negation, and copy_sign() for copying sign from another decimal ?

import decimal

my_dec = decimal.Decimal(-25.36)
print('Original:', my_dec)

# Copy absolute value
abs_dec = my_dec.copy_abs()
print('Absolute:', abs_dec)

# Copy negated value
pos_dec = decimal.Decimal(7.26)
neg_dec = pos_dec.copy_negate()
print('Negate of 7.26:', neg_dec)

# Copy sign from second argument
sign_dec = my_dec.copy_sign(decimal.Decimal(12.5))
print('Copy sign from positive number:', sign_dec)
Original: -25.3599999999999994315658113919198513031005859375
Absolute: 25.3599999999999994315658113919198513031005859375
Negate of 7.26: -7.2599999999999997868371792719699442386627197265625
Copy sign from positive number: 25.3599999999999994315658113919198513031005859375

Maximum and Minimum Methods

The max() and min() methods find the maximum and minimum values between two decimal numbers ?

import decimal

my_dec1 = decimal.Decimal(5.3)
my_dec2 = decimal.Decimal(-9.23)

print('First number:', my_dec1)
print('Second number:', my_dec2)
print('Minimum:', my_dec1.min(my_dec2))
print('Maximum:', my_dec1.max(my_dec2))
First number: 5.29999999999999982236431605997495353221893310546875
Second number: -9.230000000000000426325641456060111522674560546875
Minimum: -9.230000000000000426325641456060111522674560546875
Maximum: 5.29999999999999982236431605997495353221893310546875

Conclusion

The Decimal module provides precise arithmetic operations essential for financial and scientific calculations. Its methods offer mathematical functions, comparison operations, and copy utilities that maintain decimal precision without floating-point errors.

Updated on: 2026-03-25T04:54:33+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements