The Wayback Machine - http://web.archive.org/web/20240926174829/https://www.geeksforgeeks.org/decimal-functions-python-set-1/
Open In App

Decimal Functions in Python | Set 1

Last Updated : 22 Jan, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Python in its definition provides certain methods to perform faster decimal floating point arithmetic using the module “decimal”. 
Important operations on Decimals
1. sqrt() :- This function computes the square root of the decimal number.
2. exp() :- This function returns the e^x (exponent) of the decimal number.
 

Python3




# Python code to demonstrate the working of
# sqrt() and exp()
 
# importing "decimal" module to use decimal functions
import decimal
 
# using exp() to compute the exponent of decimal number
a = decimal.Decimal(4.5).exp()
 
# using sqrt() to compute the square root of decimal number
b = decimal.Decimal(4.5).sqrt()
 
# printing the exponent
print ("The exponent of decimal number is : ",end="")
print (a)
 
# printing the square root
print ("The square root of decimal number is : ",end="")
print (b)


Output: 
 

The exponent of decimal number is : 90.01713130052181355011545675
The square root of decimal number is : 2.121320343559642573202533086

3. ln() :- This function is used to compute natural logarithm of the decimal number.
4. log10() :- This function is used to compute log(base 10) of a decimal number.
 

Python3




# Python code to demonstrate the working of
# ln() and log10()
 
# importing "decimal" module to use decimal functions
import decimal
 
# using ln() to compute the natural log of decimal number
a = decimal.Decimal(4.5).ln()
 
# using sqrt() to compute the log10 of decimal number
b = decimal.Decimal(4.5).log10()
 
# printing the natural logarithm
print ("The natural logarithm of decimal number is : ",end="")
print (a)
 
# printing the log10
print ("The log(base 10) of decimal number is : ",end="")
print (b)


Output: 
 

The natural logarithm of decimal number is : 1.504077396776274073373258352
The log(base 10) of decimal number is : 0.6532125137753436793763169118

5. as_tuple() :- Returns the decimal number as tuple containing 3 arguments, sign(0 for +, 1 for -), digits and exponent value.
6. fma(a,b) :- This “fma” stands for fused multiply and add. It computes (num*a)+b from the numbers in argument. No rounding of (num*a) takes place in this function.
Example : 
 

decimal.Decimal(5).fma(2,3) --> (5*2)+3 = 13

 

Python3




# Python code to demonstrate the working of
# as_tuple() and fma()
 
# importing "decimal" module to use decimal functions
import decimal
 
# using as_tuple() to return decimal number as tuple
a = decimal.Decimal(-4.5).as_tuple()
 
# using fma() to compute fused multiply and addition
b = decimal.Decimal(5).fma(2,3)
 
# printing the tuple
print ("The tuple form of decimal number is : ",end="")
print (a)
 
# printing the fused multiple and addition
print ("The fused multiply and addition of decimal number is : ",end="")
print (b)


Output: 
 

The tuple form of decimal number is : DecimalTuple(sign=1, digits=(4, 5), exponent=-1)
The fused multiply and addition of decimal number is : 13

7. compare() :- This function is used to compare 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.
8. compare_total_mag() :- Compares the total magnitude of decimal numbers. Returns 1 if 1st Decimal argument is greater than 2nd(ignoring sign), -1 if 1st Decimal argument is smaller than 2nd(ignoring sign) and 0 if both are equal(ignoring sign).
 

Python3




# Python code to demonstrate the working of
# compare() and compare_total_mag()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(9.53)
 
# Initializing decimal number
b = decimal.Decimal(-9.56)
 
# comparing decimal numbers using compare()
print ("The result of comparison using compare() is : ",end="")
print (a.compare(b))
 
# comparing decimal numbers using compare_total_mag()
print ("The result of comparison using compare_total_mag() is : ",end="")
print (a.compare_total_mag(b))


Output: 
 

The result of comparison using compare() is : 1
The result of comparison using compare_total_mag() is : -1

9. copy_abs() :- This function prints the absolute value of decimal argument.
10. copy_negate() :- This function prints the negation of decimal argument.
11. copy_sign() :- This function prints the first argument by copying the sign from 2nd argument.
 

Python3




# Python code to demonstrate the working of
# copy_abs(),copy_sign() and copy_negate()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(9.53)
 
# Initializing decimal number
b = decimal.Decimal(-9.56)
 
# printing absolute value using copy_abs()
print ("The absolute value using copy_abs() is : ",end="")
print (b.copy_abs())
 
# printing negated value using copy_negate()
print ("The negated value using copy_negate() is : ",end="")
print (b.copy_negate())
 
# printing sign effected value using copy_sign()
print ("The sign effected value using copy_sign() is : ",end="")
print (a.copy_sign(b))


Output: 
 

The absolute value using copy_abs() is : 9.5600000000000004973799150320701301097869873046875
The negated value using copy_negate() is : 9.5600000000000004973799150320701301097869873046875
The sign effected value using copy_sign() is : -9.5299999999999993605115378159098327159881591796875

12. max() :- This function computes the maximum of two decimal numbers.
13. min() :- This function computes the minimum of two decimal numbers.
 

Python3




# Python code to demonstrate the working of
# min() and max()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(9.53)
 
# Initializing decimal number
b = decimal.Decimal(7.43)
 
# printing minimum of both values
print ("The minimum of two numbers is : ",end="")
print (a.min(b))
 
# printing maximum of both values
print ("The maximum of two numbers is : ",end="")
print (a.max(b))


Output: 
 

The minimum of two numbers is : 7.429999999999999715782905696
The maximum of two numbers is : 9.529999999999999360511537816

 

 

 

 



Similar Reads

Decimal Functions in Python | Set 2 (logical_and(), normalize(), quantize(), rotate() ... )
Some of the Decimal functions have been discussed in Set 1 below Decimal Functions in Python | Set 1 More functions are discussed in this article.1. logical_and() :- This function computes digit-wise logical "and" operation of the number. Digits can only have the values 0 or 1. 2. logical_or() :- This function computes digit-wise logical "or" opera
6 min read
Mathematical Functions in Python | Set 1 (Numeric Functions)
In python a number of mathematical operations can be performed with ease by importing a module named "math" which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number is returned. 2. floor() :- This function returns t
3 min read
Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Mathematical Functions in Python | Set 4 (Special Functions and Constants)
Some of the mathematical functions are discussed in below set 1, set 2 and set 3 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Special Functions and constants are discussed in this
2 min read
Convert Decimal to Other Bases in Python
Given a number in decimal number convert it into binary, octal and hexadecimal number. Here is function to convert decimal to binary, decimal to octal and decimal to hexadecimal. Examples: Input : 55 Output : 55 in Binary : 0b110111 55 in Octal : 0o67 55 in Hexadecimal : 0x37 Input : 282 Output : 282 in Binary : 0b100011010 282 in Octal : 0o432 282
2 min read
Python | Decimal compare() method
Decimal#compare() : compare() is a Decimal class method which compares the two Decimal values. Syntax: Decimal.compare() Parameter: Decimal values Return: 1 - if a > b -1 - if a < b 0 - if a = b Code #1 : Example for compare() method # Python Program explaining # compare() method # loading decimal library from decimal import * # Initializing
2 min read
Python | Decimal is_subnormal() method
Decimal#is_subnormal() : is_subnormal() is a Decimal class method which checks whether the Decimal value is subnormal. Syntax: Decimal.is_subnormal() Parameter: Decimal values Return: true - if the Decimal value is subnormal otherwise false Code #1 : Example for is_subnormal() method # Python Program explaining # is_subnormal() method # loading dec
2 min read
Python | Decimal logb() method
Decimal#logb() : logb() is a Decimal class method which returns the adjusted exponent of the Decimal value. Syntax: Decimal.logb() Parameter: Decimal values Return: the adjusted exponent of the Decimal value. Code #1 : Example for logb() method # Python Program explaining # logb() method # loading decimal library from decimal import * # Initializin
2 min read
Python | Decimal conjugate() method
Decimal#conjugate() : conjugate() is a Decimal class method which returns the self, this method is only to comply with the Decimal Specification Syntax: Decimal.conjugate() Parameter: Decimal values Return: the self Decimal value Code #1 : Example for conjugate() method # Python Program explaining # conjugate() method # loading decimal library from
2 min read
Python | Decimal copy_sign() method
Decimal#copy_sign() : copy_sign() is a Decimal class method which returns the copy of the first Decimal value with the sign set to be the same as the sign of the second Decimal value. Syntax: Decimal.copy_sign() Parameter: Decimal values Return: the copy of the first Decimal value with the sign set to be the same as the sign of the second Decimal v
2 min read
Python | Decimal compare_total() method
Decimal#compare_total() : compare_total() is a Decimal class method which compares the two Decimal values using their abstract representation rather than their numerical value. Syntax: Decimal.compare_total() Parameter: Decimal values Return: 1 - if a > b -1 - if a < b 0 - if a = b Code #1 : Example for compare_total() method # Python Program
2 min read
Python | Decimal compare_total_mag() method
Decimal#compare_total_mag() : compare_total_mag() is a Decimal class method which compares the two Decimal values by using their abstract representation rather than their value, but ignoring the sign of each operand. Syntax: Decimal.compare_total_mag() Parameter: Decimal values Return: 1 - if a > b -1 - if a < b 0 - if a = b Code #1 : Example
2 min read
Python | Decimal is_signed() method
Decimal#is_signed() : is_signed() is a Decimal class method which checks whether the sign of Decimal value is negative Syntax: Decimal.is_signed() Parameter: Decimal values Return: true - if the sign of Decimal value is negative; otherwise false Code #1 : Example for is_signed() method # Python Program explaining # is_signed() method # loading deci
2 min read
Python | Decimal is_normal() method
Decimal#is_normal() : is_normal() is a Decimal class method which checks whether the Decimal value is a normal finite number. Syntax: Decimal.is_normal() Parameter: Decimal values Return: true - if the Decimal value is a normal finite number; otherwise false Code #1 : Example for is_normal() method # Python Program explaining # is_normal() method #
2 min read
Python | Decimal adjusted() method
Decimal#adjusted() : adjusted() is a Decimal class method which returns the adjusted exponent after shifting out the coefficient’s rightmost digits until only the lead digit remains Syntax: Decimal.adjusted() Parameter: Decimal values Return: the adjusted exponent after shifting out the coefficient’s rightmost digits until only the lead digit remai
2 min read
Python | Decimal as_integer_ratio() method
Decimal#as_integer_ratio() : as_integer_ratio() is a Decimal class method which returns the exponent after shifting out the coefficient’s rightmost digits until only the lead digit remains : as a pair (n, d) Syntax: Decimal.as_integer_ratio() Parameter: Decimal values Return: the exponent after shifting out the coefficient’s rightmost digits until
2 min read
Python | Decimal is_qnan() method
Decimal#is_qnan() : is_qnan() is a Decimal class method which checks whether the Decimal value is quite NaN value. Syntax: Decimal.is_qnan() Parameter: Decimal values Return: true - if the Decimal value is quite NaN value; otherwise false Code #1 : Example for is_qnan() method # Python Program explaining # is_qnan() method # loading decimal library
2 min read
Python | Decimal as_tuple() method
Decimal#as_tuple() : as_tuple() is a Decimal class method which returns named tuple representation of the Decimal value. Syntax: Decimal.as_tuple() Parameter: Decimal values Return: tuple with values - sign - digits - exponent Code #1 : Example for as_tuple() method # Python Program explaining # as_tuple() method # loading decimal library from deci
2 min read
Python | Decimal compare_signal() method
Decimal#compare_signal() : compare_signal() is a Decimal class method which compares th two Decimal values, except for the NaN values. Syntax: Decimal.compare_signal()Parameter: Decimal valuesReturn: 1 - if a > b -1 - if a < b 0 - if a = b Code #1 : Example for compare_signal() method C/C++ Code # Python Program explaining # compare_signal()
2 min read
Python | Decimal canonical() method
Decimal#canonical() : canonical() is a Decimal class method which returns the canonical encoding of the Decimal value. Syntax: Decimal.canonical() Parameter: Decimal values Return: the canonical encoding of the Decimal value. Code #1 : Example for canonical() method # Python Program explaining # canonical() method # loading decimal library from dec
2 min read
Python | Decimal is_nan() method
Decimal#is_nan() : is_nan() is a Decimal class method which checks whether the Decimal value is NaN value. Syntax: Decimal.is_nan() Parameter: Decimal values Return: true - if the Decimal value is NaN value; otherwise false Code #1 : Example for is_nan() method # Python Program explaining # is_nan() method # loading decimal library from decimal imp
2 min read
Python | Decimal is_infinite() method
Decimal#is_infinite() : is_infinite() is a Decimal class method which checks whether the Decimal value is infinite value. Syntax: Decimal.is_infinite() Parameter: Decimal values Return: true - if the Decimal value is infinite value; otherwise false Code #1 : Example for is_infinite() method # Python Program explaining # is_infinite() method # loadi
2 min read
Python | Decimal is_finite() method
Decimal#is_finite() : is_finite() is a Decimal class method which checks whether the Decimal value is finite value. Syntax: Decimal.is_finite() Parameter: Decimal values Return: true - if the Decimal value is finite value; otherwise false Code #1 : Example for is_finite() method # Python Program explaining # is_finite() method # loading decimal lib
2 min read
Python | Decimal logical_invert() method
Decimal#logical_invert() : logical_invert() is a Decimal class method which returns the digit-wise inversion of the Decimal values. Syntax: Decimal.logical_invert() Parameter: Decimal values Return: the digit-wise inversion of the Decimal values. Code #1 : Example for logical_invert() method # Python Program explaining # logical_invert() method # l
2 min read
Python | Decimal logical_xor() method
Decimal#logical_xor() : logical_xor() is a Decimal class method which returns the digit-wise exclusive or of the two Decimal values. Syntax: Decimal.logical_xor() Parameter: Decimal values Return: the digit-wise and of the two (logical) Decimal values. Code #1 : Example for logical_xor() method # Python Program explaining # logical_xor() method # l
2 min read
Python | Decimal is_canonical() method
Decimal#is_canonical() : is_canonical() is a Decimal class method which checks whether the Decimal value is canonical. Syntax: Decimal.is_canonical() Parameter: Decimal values Return: true - if the Decimal value is canonical; otherwise false Code #1 : Example for is_canonical() method # Python Program explaining # is_canonical() method # loading de
2 min read
Python | Decimal from_float() method
Decimal#from_float() : from_float() is a Decimal class method which converts a float to a decimal number, exactly. Syntax: Decimal.from_float() Parameter: Decimal values Return: converts a float to a decimal number, exactly. Code #1 : Example for from_float() method C/C++ Code # Python Program explaining # from_float() method # loading decimal libr
2 min read
Python | Decimal copy_negate() method
Decimal#copy_negate() : copy_negate() is a Decimal class method which returns the negation of Decimal value. Syntax: Decimal.copy_negate() Parameter: Decimal values Return: the negation of Decimal value Code #1 : Example for copy_negate() method # Python Program explaining # copy_negate() method # loading decimal library from decimal import * # Ini
2 min read
Python | Decimal exp() method
Decimal#exp() : exp() is a Decimal class method which returns the value of the (natural) exponential function e**x at the given number Syntax: Decimal.exp() Parameter: Decimal values Return: the value of the (natural) exponential function e**x at the given number Code #1 : Example for exp() method # Python Program explaining # exp() method # loadin
2 min read
Article Tags :
Practice Tags :