  # Decimal Functions in Python | Set 1

File handling | Python Methods and Functions

Important decimal point operations

1. sqrt () : — This function calculates the square root of a decimal number.

2. exp () : — This function returns e ^ x (exponent) decimal.

 ` # Python code to demonstrate how it works ` ` # sqrt () and exp () `   ` # import & quot; decimal & quot; module for using decimal functions ` ` import ` ` decimal `   ` # using exp () to calculate the decimal power ` ` a ` ` = ` ` decimal.Decimal (` ` 4.5 ` `). exp () `   ` # using sqrt () to calculate the square root of a decimal ` ` b ` ` = ` ` decimal.Decimal (` ` 4.5 ` `). sqrt () `   ` # print exponent ` ` print ` ` (` ` "The exponent of decimal numbe r is: "` `, end ` ` = ` `" ") ` ` print ` ` (a) ` ` `  ` # printing 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 calculate the natural logarithm decimal number.

4. log10 () : — This function is used to calculate the log (base 10) decimal.

 ` # Python code to demonstrate how it works ` ` # ln () and log10 () `   ` # import & quot; decimal & quot; module for using decimal functions ` ` import ` ` decimal `   ` # using ln () to calculate the natural logarithm of a decimal number ` ` a ` ` = ` ` decimal.Decimal (` ` 4.5 ). ln () ``   # using sqrt () to calculate the log10 decimal b = decimal.Decimal ( 4.5 ). log10 ()   # print natural logarithm print ( "The natural l ogarithm of decimal number is: " , end = " ") print (a)    # print 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 a decimal number as a tuple containing 3 arguments, sign (0 for +, 1 for -), digits, and an exponent .

6. fma (a, b) : — this "fma" means merged multiply and add . It calculates (num * a) + b from the numbers in the argument.  In this function no rounding (num * a) is performed.

Example:

` decimal . Decimal (5). Fma (2,3) - & gt; (5 * 2) +3 = 13 `

` `

` # Python code to demonstrate how it works # as_tuple () and fma ()   # import & quot; decimal & quot; module for using decimal functions import decimal   # using as_tuple () to return a decimal number as a tuple a = decimal.Decimal ( - 4.5 ). as_tuple ()   # using fma () to compute merged multiplication and addition b = decimal.Decimal ( 5 ) .fma ( 2 , 3 )   # printing a tuple print ( "The tuple form of decimal number is:" , end = " ") print (a)   # print merged set 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 the 1st decimal argument is greater than the 2nd, -1 if the 1st decimal argument is less than the 2nd, and 0 if both are equal.       8. compare_total_mag () : — compares the total value of decimal numbers.  Returns 1 if the 1st decimal argument is greater than the 2nd (ignoring the sign), -1 if the 1st decimal argument is less than the 2nd (ignoring the sign), and 0 if both are equal (ignoring the sign). ` ``       # Python code to demonstrate how it works    # compare () and compare_total_mag ()       # import "decimal" module for using decimal functions     import   decimal      # Initialize decimal     a   =   decimal.Decimal (  9.53  )       # Initialize decimal     b   =   decimal.Decimal (  -   9.56  )       # decimal comparison using comparison ()     print   (  "The result of comparison using compare ( ) is: " , end   =  " ")     print   (a.compare (b))      # compare 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 the decimal argument.    10. copy_negate () : — This function prints the  negation  of the decimal argument.    11. copy_sign () : — This function prints the  first argument, copying the character from the second argument .            # Python code to demonstrate how it works    # copy_abs (), copy_sign () and copy_negate ()       # import decimal module for using decimal functions     import   decimal      # Initialize decimal     a   =   decimal.Decimal (  9.53  )       # Initialize decimal     b   =   decimal.Decimal (  -   9.56  )       # print the absolute value using copy_abs ()     print   (  "The absolute value using copy_ab s () is: " , end   =  " ")     print   (b.copy_abs ())      # print negative value using copy_negate ()     print   (  "The negated value using copy_negate () is:"   , end   =   "")    print   (b.copy_negate ())      # print the entered sign value using copy_sign ()     print   (  "The sign effected value using copy_sign () is:"  , end   =  " ")    print   (a.copy_sign (b)) `  Exit:   The absolute value using copy_abs () is: 9.5600000000000004973799150320701301097869873046875 The negated value using copy_negate () is: 9.5600000000000004973799150320701301097869873046875 The sign effected value601097869873046875 The sign effected value601097869873046875 The sign effected value60: copy_sign () > 12. max () : — this function calculates a  maximum  of two decimal numbers.    13. min () : — This function calculates the  minimum  of two decimal numbers.           ` # Python code to demonstrate how it works `  ` # min () and max () `     ` # import decimal module for using decimal functions `   ` import ` ` decimal `    ` # Initialize decimal `   ` a ` ` = ` ` decimal.Decimal (` ` 9.53 ` `) `  ` `  ` # Initialize decimal `   ` b ` ` = ` ` decimal.Decimal (` ` 7.43 ` `) `    ` # print the minimum of both values ​​`   ` print ` ` (` ` "The minimum of two numbers is:" ` `, end ` ` = ` `" ") `  ` print ` ` (a. ` ` min ` ` (b)) `    ` # print the 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   This article is courtesy of   Mange it Singh  . If you are as Python.Engineering and would like to contribute, you can also write an article using  contribute.python.engineering  or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.   Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.