Python Decimal Module

Python Methods and Functions

In this article we will look at one of the interesting modules &Python Decimal module.

We are often faced with the need to find functions to perform mathematical operations. The Python decimal module provides all the math functions we need.

 decimal modulus



< / p >


What is a decimal module in Python?

Pyth's decimal module on contains various functions for processing numeric data and performing various mathematical operations on it. Using the decimal module, we can efficiently handle decimal numbers in a program.

The decimal module provides us with functions to manage and overcome the problem of decimal precision.

To to use functions, we need to import the module like below:

 import decimal 



Functions and implementation

Various arithmetic operations can be performed on decimal or numeric data to improve the result.

We can define numbers with decimal points using the decimal.Decimal () function as shown below.

Syntax:

 import decimal variable = decimal.Decimal (decimal-number) 

Also , we can control the precision value of the results of decimal point numbers using the built-in decimal unit function & decimal.getcontext (). prec .

Syntax:

 decimal.getcontext () .prec = precision value 

The functions described below help us perform decimal point arithmetic efficiently and at high speed.




The exp () function &calculating the exponent

The exp () function calculates the exponent, that is, e ^ x of the specified number of the passed decimal point.

Syntax:

 decimal.Decimal (decimal-number) .exp () 

Example:

import decimal as d d.getcontext (). prec = 5 #Intializing with an addition operation val = d.Decimal (12.201) + d.Decimal (12.20) #Calculating exponential of the decimal value exp = val.exp () #variabl e with no calculations no_math = d.Decimal (1.131231) print ("Sum:", val) print ("Exponential:", exp) print (no_math)

Output:

 Decimal Number: 24.401 3.9557E + 10 1.131231000000000097571728474576957523822784423828125 

Note that the total number of digits in our output is 5. This is due to the precision we set here.

Remember that the precision value applies when you are doing math with two decimal places, not when you directly instantiate a variable with values ​​as shown with the "no_math" variable above.




Function sqrt () &square root

The sqrt () function calculates and returns the square root of the passed decimal number.

Syntax:

 decimal.Decimal (decimal-number) .sqrt () 

Example :



 import decimal as d d.getcontext (). prec = 3 val = d.Decimal (122.20) sqrt = val.sqrt () print ("Decimal Number: ", val) print (" Square root of the decimal number: ", sqrt) 

Output:

 Decimal Number : 122.2000000000000028421709430404007434844970703125 Square root of the decimal number: 11.1 

Again, notice how the declared value contains the full decimal number, while the calculated value matches our 3-digit precision set.




3 Logarithmic functions

The module provides us with the following functions for calculating logarithmic their values ​​are numbers with a decimal point:

  • decimal.ln ();
  • decimal.log10 ().

decimal.ln () function returns a natural decimal number as shown below.

 decimal.Decimal (decimal-number) .ln () 

The decimal.log10 () function is used to calculate the log value based on the base 10 decimal number passed to it.

 decimal.Decimal (decimal-number) .log10 () 

Example:

 import decimal as d d.getcontext () .prec = 2 val = d.Decimal (122.20) log = val.ln () print ("Natural log value of the decimal number:", log) log_10 = val.log10 () print ("Log value with base 10 of the decimal num ber: ", log_10) 

Output:

 Natural log value of the decimal number: 4.8 Log value with base 10 of the decimal number: 2.1 



The compare () function

The decimal.compare () function compares two numbers with a decimal point and returns the values depending on the conditions as follows:

  • Returns -1 if the first decimal is less than the second decimal.
  • Returns 1 if the first decimal is greater than the second decimal. < / li>
  • Returns 0 if both decimal point values ​​are equal.

Example:

 import decimal as d valx = d.Decimal (122.20) valy = d.Decimal (123.01) print ("Value 1:", valx) print ("Value 2:", valy) compare = valx.compare (valy) print ( compare) 

Output:

 Value 1: 122.2000000000000028421709430404007434844970703125 Value 2: 123.0100000000000051159076974727213382720947265625> -12pre Function copy_abs ()  

Function decimal.copy_abs () function returns the absolute values ​​of the signed decimal number passed to it.

Syntax:

 decimal.Decimal (signed decimal number) .copy_abs () 

Example:

 import decimal as d valx = d.Decimal (-122.20) print ("Value 1:", valx) absolute = valx.copy_abs () print ("Absolute value of the given decimal number:", absolute) 

Output:

 Value 1: -122.2000000000000028421709430404007434844970703125 Absolute value of the given decimal number: 122.2000000000000028421709430404007434844970703125 



The maximum and minimum modulus

a href = "https://pythononline.ru/osnovy/max-min-python"> calculate the minimum and maximum values ​​ decimal point.

  • min () function: returns the minimum two decimal values d.
  • max () function: Returns the maximum of two decimal values.
 # Syntax for min () function- decimal1.min (decimal2) #Syntax for max () function- decimal1.max (decimal2) 

Example:

 import decimal as d valx = d.Decimal (122.20) valy = d.Decimal (123.01) print ("Value 1:", valx) print ("Value 2:", valy) min_val = valx.min ( valy) print ("The minimum of the two values:", min_val) max_val = valx.max (valy) print ("The maximum of the two values:", max_val) 

Output:

 Value 1: 122.2000000000000028421709430404007434844970703125 Value 2: 123.0100000000000051159076974727213382720947265625 the minimum of the two values : 122.2000000000000028421709430 the maximum of the two values: 123.01000000000000511590 76975 



Logical operations with decimal modulus

Decimal code Holds a set of built-in functions for performing logical operations on decimal numbers such as AND, OR, XOR, etc.

  • logical_and () function: Performs a logical AND operation on two decimal numbers and returns the result < / li>
 # Syntax for logical_and () function- decimal1.logical_and (decimal2) #Syntax for logical_or () function- decimal1.logical_or ( decimal2) #Syntax for logical_xor () function- decimal1.logical_xor (decimal2) 

Example:

 import decimal as d valx = d.Decimal (1001) valy = d.Decimal (1111) print ("Value 1:", valx) print ("Value 2:", va ly) AND = valx.logical_and (valy) print ("The logical AND value of the two decimals: ", AND) OR = valx.logical_or (valy) print (" The logical OR value of the two decimals: ", OR) XOR = valx.logical_xor (valy) print (" The logical XOR value of the two decimals: ", XOR ) 

Output:

 Value 1: 1001 Value 2: 1111 The logical AND value of the two decimals: 1001 The logical OR value of the two decimals: 1111 The logical XOR value of the two decimals: 110  







Get Solution for free from DataCamp guru