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.
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.
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.
decimal.Decimal(5).fma(2,3) --> (5*2)+3 = 13
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 magnitute 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).
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.
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.
The minimum of two numbers is : 7.429999999999999715782905696 The maximum of two numbers is : 9.529999999999999360511537816
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python
- Interesting facts about strings in Python | Set 1
- When to use yield instead of return in Python?