Decimal Functions in Python | Set to 2 (logic_and (), normalize (), quantize (), rotate () …)

# Python code to demonstrate how it works
# logic_and (), logic_or (), logic_xor ()
# and logic_invert ()

 
# import & quot; decimal & quot; module for using decimal functions

import decimal

 
# Initialize decimal

a = decimal.Decimal ( 1000 )

  
# Initialize decimal

b = decimal.Decimal ( 1110 )

 
# printing a boolean_and two numbers

print ( "The logical_and () of two numbers is:" , end = "")

print (a.logical_and (b))

 
# print logical_or two numbers

print ( "The logical_or () of two numbers is: " , end = " " )

print (a.logical_or (b))

 
# printing an exclusive or two numbers

print ( " The exclusive or of two numbers is: " , end = "")

print (a.logical_xo r (b))

 
# print the logical inverse of the number

print ( "The logical inversion of number is:" , end = "")

print (a.logical_invert ())

Output:

 The logical_and () of two numbers is: 1000 The logical_or () of two numbers is: 1110 The exclusive or of two numbers is: 110 The logical inversion of number is: 11111111111111111111110111 

5. next_plus () : — This function returns the smallest number that can be represented, greater than the specified number.

6. next_plus () : — This function returns the largest number that can be represented, less than the specified number.

# Python- demo code
# next_plus () and next_minus ()

 
# import & quot; decimal & quot; module for using decimal functions

import decimal

 
# Initialize decimal

a = decimal.Decimal ( 101.34 )

  
# print actual decimal

print ( "The original number is:" , end = "")

print (a)

 
# print number after using next_plus ()

print ( "The smallest number larger than current number:" , end = "")

print (a.next_plus ())

 
# print number after using next_minus ()

print ( "The largest number smaller than current number:" , end = " ")

print (a.next_minus ())

Output:

 The original number is: 101.340000000000003410605131648480892181396484375 The smallest number larger than current number: 101.3400000000000034106051317 The largest number smaller t han current number: 101.3400000000000034106051316 

7. next_toward () : — This function returns the number closest to the first argument in the direction of the second argument. If both numbers are equal, it returns the second signed number of the first number.

8. normalize () : — This function prints the number after erasing all rightmost zeros in the number.

# Python code for demonstrations
# next_toward () and normalize ()

 
# import "decimal" module for using decimal functions

import decimal

 
# Initialize decimal

a = decimal.Decimal ( 101.34 )

  
# Initialize decimal

b = decimal.Decimal ( - 101.34 )

  
# Initialize decimal

c = decimal.Decimal ( - 58.68 )

 
# Initialize decimal

d = decimal.Decimal ( 14.010000000 )

 
# print number using next_toward ()

print ( "The number closest to 1st number in direction of second number:" )

print (a.next_toward (c))

 
# print number using next_toward ()
# when equal

print ( "The second number with sign of first number is:" , end = "")

print (b.next_toward (a))

 
# number printing after removing the rightmost zeros

print ( "Number after erasing rightmost trailing zeroes:" , end = " ")

print (d.normalize ())

Output:

 The number closest to 1st number in direction of second number: 101.3400000000000034106051316 The second number with sign of first number is: -101.3400000000000034106051316 Number after erasing rightmost trailing zeroes: 14.01 

9. quantize () : — This function returns the 1st argument with the number of digits in the decimal part (exponent) truncated by the number of digits in the decimal part (exponent) of the 2nd argument.

10. same_quantum () : — this function returns 0 if both numbers have different rates, and 1 if both numbers have the same rates.

# Python- demo code
# quantize () and same_quantum ()

 
# import & quot; decimal & quot; module for using decimal functions

import decimal

 
# Initialize decimal

a = decimal.Decimal ( 20.76548 )

  
# Initialize decimal

b = decimal.Decimal ( 12.25 )

 
# Initialize decimal

c = decimal.Decimal ( 6.25 )

 
# print the quantized first number

print ( "The quantized first number is:" , end = "")

print (a.quantize (b))

 
# check if both numbers have the same exponent

if (b.same_quantum (c )):

print ( "Both the numbers have same exponent" )

else : print ( “Both numbers have different exponent”

Exit:

 The quantized first number is: 20.77 Both the numbers have same exponent 

eleven. rotate () : — this function rotates the first argument by the amount specified in the second . If the sign of the second argument is positive, the rotation is to the left , otherwise the rotation is to the right . The sign of the first argument is unchanged.

12. shift () : — this function shifts the first argument by the amount specified in the second . If the sign of the second argument is positive, offset to the left, otherwise, offset to the right . The sign of the first argument is unchanged. Shifted digits are replaced with 0 .

# Python code to demonstrate how it works
# rotate () and shift ()

 
# import & quot; decimal & quot; module for using decimal functions

import decimal

 
# Initialize decimal

a = decimal.Decimal ( 2343509394029424234334563465 )

  
# using rotate () to rotate the first argument
# rotate right by 2 positions

print ( " The rotated value is: " , end = "")

print (a.rotate ( - 2 ))

 
# using shift () to shift the first argument
# rotate left 2 positions

print ( "The shifted value is:" , end = "")

print (a.shift ( 2 ))

Output:

 The rotated value is: 6523435093940294242343345634 The shifted value is: 4350939402942423433456346500 

13. remainder_near () : — Returns the value “ 1st — (n * 2nd) “, where n — it is the integer value closest to the 1st / 2nd result. If 2 integers are equally close, even one is selected.

14. scaleb () : — this function shifts the exponent of the 1st number by the value of the second argument .

# Python code to demonstrate how it works
# remainder_near () and scaleb ()

  
# import & quot; decimal & quot; module for using decimal functions

import decimal

 
# Initialize decimal

a = decimal.Decimal ( 23.765 )

  
# Initialize decimal

b = decimal.Decimal ( 12 )

 
# Initialize decimal

c = decimal.Decimal ( 8 )

  
# using remainder_near to calculate the value

print ( "The computed value using remainder_near () is:" , end = "")

print (b.remainder_near (c))

 
# using scaleb () to shift the exponent

print ( "The value after shifting exponent:" , end = " ")

print (a.scaleb ( 2 ))

Output:

 The computed value using remainder_near () is: -4 The value after shifting exponent: 2376.500000000000056843418861 

This article courtesy of Manjit 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.