Python 内建函数 - pow(x, y[, z])

Manual

Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z). The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

The arguments must have numeric types. With mixed operand types, the coercion rules for binary arithmetic operators apply. For int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, 10**2 returns 100, but 10**-2 returns 0.01. If the second argument is negative, the third argument must be omitted. If z is present, x and y must be of integer types, and y must be non-negative.

直译

返回xy次方;如果存在z,返回xy次方的z模(比计算pow(x, y) % z更有效)。构成pow(x, y)的两个参数等于使用幂运算:x**y。
参数必须是数值类型。针对二进制算术运算符的强制规则,应用混合运算数类型。对于整型运算数,结果与运算数的类型相同,除非第二个参数是负数,这种情况下,所有参数转化为浮点型,并返回浮点型结果。例如:10**2返回100,但10**-2返回0.01。如果第二个参数是负值,则第三个参数必须缺省。如果出现zxy必须是整数类型,且y必须为非负。

实例

>>> pow(3, 2)
9
>>> pow(3, 2, 5)
4
>>> 3 ** 2 % 5
4
>>> pow(3, 0.2, 5)
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    pow(3, 0.2, 5)
TypeError: pow() 3rd argument not allowed unless all arguments are integers

>>> pow(100, -2)
0.0001
>>> pow(10, -2)
0.01
>>> pow(10, -2, 3)
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    pow(10, -2, 3)
ValueError: pow() 2nd argument cannot be negative when 3rd argument specified
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值