📜  Python中的小数函数 |设置 2 (logical_and(), normalize(), quantize(), rotate() ... )

📅  最后修改于: 2022-05-13 01:54:54.330000             🧑  作者: Mango

Python中的小数函数 |设置 2 (logical_and(), normalize(), quantize(), rotate() ... )

一些 Decimal 函数已在下面的 Set 1 中讨论

Python中的小数函数 |设置 1

本文讨论了更多功能。
1.logical_and() :- 该函数计算数字的按位逻辑“与”运算。数字只能具有值0 或 1

2.logical_or() :- 此函数计算数字的逐位逻辑“或”运算。数字只能具有值0 或 1

3.logical_xor() :- 该函数计算数字的逐位逻辑“xor”运算。数字只能具有值0 或 1

4.logical_invert() :- 该函数计算数字的逐位逻辑“反转”运算。数字只能具有值0 或 1

Python3
# Python code to demonstrate the working of
# logical_and(), logical_or(), logical_xor()
# and logical_invert()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(1000)
 
# Initializing decimal number
b = decimal.Decimal(1110)
 
# printing logical_and of two numbers
print ("The logical_and() of two numbers is : ",end="")
print (a.logical_and(b))
 
# printing logical_or of two numbers
print ("The logical_or() of two numbers is : ",end="")
print (a.logical_or(b))
 
# printing exclusive or of two numbers
print ("The exclusive or of two numbers is : ",end="")
print (a.logical_xor(b))
 
# printing logical inversion of number
print ("The logical inversion of number is : ",end="")
print (a.logical_invert())


Python3
# Python code to demonstrate the working of
# next_plus() and next_minus()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(101.34)
 
# printing the actual decimal number
print ("The original number is : ",end="")
print (a)
 
# printing number after using next_plus()
print ("The smallest number larger than current number : ",end="")
print (a.next_plus())
 
# printing number after using next_minus()
print ("The largest number smaller than current number : ",end="")
print (a.next_minus())


Python3
# Python code to demonstrate the working of
# next_toward() and normalize()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(101.34)
 
# Initializing decimal number
b = decimal.Decimal(-101.34)
 
# Initializing decimal number
c = decimal.Decimal(-58.68)
 
# Initializing decimal number
d = decimal.Decimal(14.010000000)
 
# printing the number using next_toward()
print ("The number closest to 1st number in direction of second number : ")
print (a.next_toward(c))
 
# printing the number using next_toward()
# when equal
print ("The second number with sign of first number is : ",end="")
print (b.next_toward(a))
 
# printing number after erasing rightmost trailing zeroes
print ("Number after erasing rightmost trailing zeroes : ",end="")
print (d.normalize())


Python3
# Python code to demonstrate the working of
# quantize() and same_quantum()
  
# importing "decimal" module to use decimal functions
import decimal
  
# Initializing decimal number
a = decimal.Decimal(20.76548)
  
# Initializing decimal number
b = decimal.Decimal(12.25)
 
# Initializing decimal number
c = decimal.Decimal(6.25)
 
# printing quantized first number
print ("The quantized first number is : ",end="")
print (a.quantize(b))
  
# checking if both number have same exponent
if (b.same_quantum(c)):
       print ("Both the numbers have same exponent")
else : print ("Both numbers have different exponent")


Python3
# Python code to demonstrate the working of
# rotate() and shift()
  
# importing "decimal" module to use decimal functions
import decimal
  
# Initializing decimal number
a = decimal.Decimal(2343509394029424234334563465)
 
# using rotate() to rotate the first argument
# rotates to right by 2 positions
print ("The rotated value is : ",end="")
print (a.rotate(-2))
  
# using shift() to shift the first argument
# rotates to left by 2 positions
print ("The shifted value is : ",end="")
print (a.shift(2))


Python3
# Python code to demonstrate the working of
# remainder_near() and scaleb()
  
# importing "decimal" module to use decimal functions
import decimal
  
# Initializing decimal number
a = decimal.Decimal(23.765)
 
# Initializing decimal number
b = decimal.Decimal(12)
 
# Initializing decimal number
c = decimal.Decimal(8)
 
# using remainder_near to compute value
print ("The computed value using remainder_near() is : ",end="")
print (b.remainder_near(c))
  
# using scaleb() to shift exponent
print ("The value after shifting exponent : ",end="")
print (a.scaleb(2))


输出:

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() :- 该函数返回可以表示的最小数字大于给定数字。

6. next_minus() :- 该函数返回可以表示的最大数字小于给定数字。

Python3

# Python code to demonstrate the working of
# next_plus() and next_minus()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(101.34)
 
# printing the actual decimal number
print ("The original number is : ",end="")
print (a)
 
# printing number after using next_plus()
print ("The smallest number larger than current number : ",end="")
print (a.next_plus())
 
# printing number after using next_minus()
print ("The largest number smaller than current number : ",end="")
print (a.next_minus())

输出:

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() :- 该函数返回在第二个参数的方向上最接近第一个参数的数字。如果两个数字相等,则返回带有第一个数字符号的第二个数字。

8. normalize() :- 此函数在删除数字中所有最右边的尾随零后打印数字。

Python3

# Python code to demonstrate the working of
# next_toward() and normalize()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(101.34)
 
# Initializing decimal number
b = decimal.Decimal(-101.34)
 
# Initializing decimal number
c = decimal.Decimal(-58.68)
 
# Initializing decimal number
d = decimal.Decimal(14.010000000)
 
# printing the number using next_toward()
print ("The number closest to 1st number in direction of second number : ")
print (a.next_toward(c))
 
# printing the number using next_toward()
# when equal
print ("The second number with sign of first number is : ",end="")
print (b.next_toward(a))
 
# printing number after erasing rightmost trailing zeroes
print ("Number after erasing rightmost trailing zeroes : ",end="")
print (d.normalize())

输出:

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() :- 此函数返回第一个参数,其中小数部分(指数)的位数被第二个参数的小数部分(指数)缩短

10. same_quantum() :- 如果两个数字具有不同的指数,则此函数返回 0,如果两个数字具有相同的指数,则返回 1。

Python3

# Python code to demonstrate the working of
# quantize() and same_quantum()
  
# importing "decimal" module to use decimal functions
import decimal
  
# Initializing decimal number
a = decimal.Decimal(20.76548)
  
# Initializing decimal number
b = decimal.Decimal(12.25)
 
# Initializing decimal number
c = decimal.Decimal(6.25)
 
# printing quantized first number
print ("The quantized first number is : ",end="")
print (a.quantize(b))
  
# checking if both number have same exponent
if (b.same_quantum(c)):
       print ("Both the numbers have same exponent")
else : print ("Both numbers have different exponent") 

输出:

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

11. rotate() :- 这个函数将第一个参数旋转第二个参数中提到的数量。如果第二个参数的符号为正,则旋转向左否则旋转向右。第一个参数的符号不变。

12. shift() :- 此函数第一个参数移动第二个参数中提到的数量。如果第二个参数的符号为正,则向左移动,否则向右移动。第一个参数的符号不变。移位的数字被替换为 0

Python3

# Python code to demonstrate the working of
# rotate() and shift()
  
# importing "decimal" module to use decimal functions
import decimal
  
# Initializing decimal number
a = decimal.Decimal(2343509394029424234334563465)
 
# using rotate() to rotate the first argument
# rotates to right by 2 positions
print ("The rotated value is : ",end="")
print (a.rotate(-2))
  
# using shift() to shift the first argument
# rotates to left by 2 positions
print ("The shifted value is : ",end="")
print (a.shift(2))

输出:

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

13.remainder_near() :- 返回值“ 1st – (n*2nd) ”,其中n 是最接近 1st/2nd 结果的整数值。如果 2 个整数具有完全相同的接近度,则甚至选择一个。

14. scaleb() :- 此函数将第一个数字的指数移动第二个参数的值

Python3

# Python code to demonstrate the working of
# remainder_near() and scaleb()
  
# importing "decimal" module to use decimal functions
import decimal
  
# Initializing decimal number
a = decimal.Decimal(23.765)
 
# Initializing decimal number
b = decimal.Decimal(12)
 
# Initializing decimal number
c = decimal.Decimal(8)
 
# using remainder_near to compute value
print ("The computed value using remainder_near() is : ",end="")
print (b.remainder_near(c))
  
# using scaleb() to shift exponent
print ("The value after shifting exponent : ",end="")
print (a.scaleb(2))

输出:

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