Decimal Functions in Python | Set 2 (logical_and(), normalize(), quantize(), rotate() … )
Last Updated :
09 Feb, 2022
Some of the Decimal functions have been discussed in Set 1 below
Decimal Functions in Python | Set 1
More functions are discussed in this article.
1. logical_and() :- This function computes digit-wise logical “and” operation of the number. Digits can only have the values 0 or 1.
2. logical_or() :- This function computes digit-wise logical “or” operation of the number. Digits can only have the values 0 or 1.
3. logical_xor() :- This function computes digit-wise logical “xor” operation of the number. Digits can only have the values 0 or 1.
4. logical_invert() :- This function computes digit-wise logical “invert” operation of the number. Digits can only have the values 0 or 1.
Python3
import decimal
a = decimal.Decimal( 1000 )
b = decimal.Decimal( 1110 )
print ( "The logical_and() of two numbers is : " ,end = "")
print (a.logical_and(b))
print ( "The logical_or() of two numbers is : " ,end = "")
print (a.logical_or(b))
print ( "The exclusive or of two numbers is : " ,end = "")
print (a.logical_xor(b))
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 : 1111111111111111111111110111
5. next_plus() :- This function returns the smallest number that can be represented, larger than the given number.
6. next_minus() :- This function returns the largest number that can be represented, smaller than the given number.
Python3
import decimal
a = decimal.Decimal( 101.34 )
print ( "The original number is : " ,end = "")
print (a)
print ( "The smallest number larger than current number : " ,end = "")
print (a.next_plus())
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 than current number : 101.3400000000000034106051316
7. next_toward() :- This function returns the number nearest to the 1st argument in the direction of the second argument. In case Both the numbers are equal, returns the 2nd number with the sign of first number.
8. normalize() :- This function prints the number after erasing all the rightmost trailing zeroes in the number.
Python3
import decimal
a = decimal.Decimal( 101.34 )
b = decimal.Decimal( - 101.34 )
c = decimal.Decimal( - 58.68 )
d = decimal.Decimal( 14.010000000 )
print ( "The number closest to 1st number in direction of second number : " )
print (a.next_toward(c))
print ( "The second number with sign of first number is : " ,end = "")
print (b.next_toward(a))
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 decimal part(exponent) shortened by the number of digits in decimal part(exponent) of 2nd argument.
10. same_quantum() :- This function returns 0 if both the numbers have different exponent and 1 if both numbers have same exponent.
Python3
import decimal
a = decimal.Decimal( 20.76548 )
b = decimal.Decimal( 12.25 )
c = decimal.Decimal( 6.25 )
print ( "The quantized first number is : " ,end = "")
print (a.quantize(b))
if (b.same_quantum(c)):
print ( "Both the numbers have same exponent" )
else : print ( "Both numbers have different exponent" )
|
Output:
The quantized first number is : 20.77
Both the numbers have same exponent
11. rotate() :- This function rotates the first argument by the amount mentioned in the second argument. If the sign of second argument is positive, rotation is towards left, else the rotation is towards right. The sign of first argument is unchanged.
12. shift() :- This function shifts the first argument by the amount mentioned in the second argument. If the sign of second argument is positive, shifting is towards left, else the shifting is towards right. The sign of first argument is unchanged. Digit shifted are replaced by 0.
Python3
import decimal
a = decimal.Decimal( 2343509394029424234334563465 )
print ( "The rotated value is : " ,end = "")
print (a.rotate( - 2 ))
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 is the integer value nearest to the result of 1st/2nd. If 2 integers have exactly similar proximity, even one is chosen.
14. scaleb() :- This function shifts the exponent of 1st number by the value of second argument.
Python3
import decimal
a = decimal.Decimal( 23.765 )
b = decimal.Decimal( 12 )
c = decimal.Decimal( 8 )
print ( "The computed value using remainder_near() is : " ,end = "")
print (b.remainder_near(c))
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...