Python | Set 2中的十进制函数(逻辑|和()、规格化()、量化()、旋转()…)

下面的集合1讨论了一些十进制函数

null

Python中的十进制函数|集1

本文将讨论更多函数。 1.逻辑_和() :-此函数按数字计算 逻辑“和” 数字的运算。数字只能有值 0或1 .

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

3.逻辑异或 :-此函数按数字计算 逻辑“异或” 数字的运算。数字只能有值 0或1 .

4.逻辑逆 :-此函数按数字计算 逻辑“反转” 数字的运算。数字只能有值 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())


输出:

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

5.下一个_plus() :-此函数返回 最小数 可以代表, 大于给定的数字。

6.下一个_减() :-此函数返回 最大数量 可以代表, 小于给定的数字。

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.340000000000003410605131648480892181396484375The smallest number larger than current number : 101.3400000000000034106051317The largest number smaller than current number : 101.3400000000000034106051316

7.下一步 :-此函数返回 在第二个参数的方向上距离第一个参数最近的数字 论点如果两个数字相等,则返回 带第一个符号的第二个数字 数字

8.正常化 :-此函数在 删除所有最右边的尾随零 在号码里。

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.3400000000000034106051316The second number with sign of first number is : -101.3400000000000034106051316Number after erasing rightmost trailing zeroes : 14.01

9.量子化() :-此函数返回带有 小数部分(指数)中的数字缩短 位数 在里面 第二个参数的小数部分(指数)。

10.同样的量子 :-这个功能 如果两个数字的指数不同,则返回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.77Both the numbers have same exponent

11.轮换 :-这个功能 旋转 第一个论点是 第二个论点中提到的金额 .如果第二个论点的标志是 正,旋转方向向左 , 否则旋转方向是向右的 .第一个论点的符号不变。

12.班次() :-这个功能 转移 第一个论点是 第二个论点中提到的金额 .如果第二个论点的标志是 正向,向左移动, 否则就要向右转 .第一个论点的符号不变。数字移位是 替换为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 : 6523435093940294242343345634The shifted value is : 4350939402942423433456346500

13.余数 :-返回值“ 第一(n*2) “在哪里 n是最接近1st/2nd结果的整数值 .如果2个整数 非常接近,甚至选择了一个。

14.scaleb() :-这个功能 移动指数 第1名 第二个参数的值 .

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 : -4The value after shifting exponent : 2376.500000000000056843418861

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享