# Decimal functions in Python

In this tutorial, you will learn about different Decimal Functions provided in Python. As we know it is difficult to perform various operations on floating-point numbers, therefore, Python provides a decimal module. This makes the task of a programmer easy and less complex.

## Decimal Functions in Python

Let’s start by learning some easy functions provided by Python.

• Basic Arithmetic Functions:
1. Square Root
2. Logarithmic Functions
3. exponent function
4. Compare Function
5. Max and Min Functions
6. Copy Functions
• Logical operations :
1. Shift and Rotate operations

### Basic Arithmetic Functions

Python provides easy to implement functions:
1. sqrt(): To find square root of a decimal number.
2. log10() : To find log to the base 10 value.
3. exp() : To find exponential value i.e e^(x).
4.  compare() : To compare two values.
5. max() and min(): To find the maximum and minimum values.
6.Copy Functions:  There three basic copy functions provided by python.

i) copy_abs() : Prints absolute value of decimal number.
ii) copy_negate() : Prints negation of the decimal number.
iii) copy_sign() : Prints first number along with sign of second number.

Here is a simple example to demonstrate the execution of the above functions.

```>>> import decimal as d
>>> d.Decimal(5.1).sqrt()
Decimal('2.258317958127242906313647036')

>>> d.Decimal(5.1).log10()
Decimal('0.7075701760979363355817870822')

>>> d.Decimal(5.1).exp()
Decimal('164.0219072999016856728608926')

>>> a= d.Decimal(4.5)
>>> b= d.Decimal(5.1)

>>> a.compare(b)
Decimal('-1')

>>> a.max(b)
Decimal('5.099999999999999644728632120')

>>> c= d.Decimal(2.3)
>>> d= d.Decimal(-4.2)

>>> c.copy_abs()
Decimal('2.29999999999999982236431605997495353221893310546875')

>>> d.copy_negate()
Decimal('4.20000000000000017763568394002504646778106689453125')

>>> c.copy_sign(d)
Decimal('-2.29999999999999982236431605997495353221893310546875')

```

### Logical Operations

Various logical operations that can be performed are :

1. Logical_and() : Perform Bit-wise ANDing of the numbers.
For example :  1001 AND 0011 = 0001
2. Logical_or(): Perform Bit-wise OR of numbers.
For example :  1001 OR 0011 = 1011
3. Logical_invert() : Perform Logical NOT of numbers.
For example :  NOT 1001 = 0110
4. Logical_xor() : Perform XOR operation on numbers.
For example :  1001 XOR 0011 = 1010
5. shift() : Positive argument => Perform Logical left shift
For example : Left shift  234556 by 2 : 4556
Negative argument => Perform Logical right shift
For example : Right shift  234556 by 2 : 2345
6. rotate() : Positive argument => Anti-Clock wise rotate digits.
For example : AntiClock rotate  234556 by 2 : 562345
Negative argument => Clockwise rotate digits.
For example: Clock rotate  234556 by 2: 455623

Here is a sample example to demonstrate the execution:

```>>> import decimal as d
>>> a= d.Decimal(1101)
>>> b= d.Decimal(1001)

>>> a.logical_and(b)
Decimal('1001')

>>> a.logical_or(b)
Decimal('1101')

>>> a.logical_invert()
Decimal('1111111111111111111111110010')

>>> a.logical_xor(b)
Decimal('100')

>>> c= d.Decimal(123456789123456789)

>>> c.shift(-2)
Decimal('1234567891234567')

>>> c.shift(2)
Decimal('12345678912345678900')

>>> c.rotate(-3)
Decimal('7890000000000123456789123456')

>>> c.rotate(3)
Decimal('123456789123456789000')```

You may also refer :