zoukankan      html  css  js  c++  java
  • Python内置函数(5)——pow

    英文文档:

    pow(xy[, z])
    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.
    说明:
      1. 函数有两个必需参数x,y和一个可选参数z,结果返回x的y次幂乘(相当于x**y),如果可选参数z有传入值,则返回幂乘之后再对z取模(相当于pow(x,y)%z)。
    >>> pow(2,5)
    32
    >>> pow(2,3)
    8
    >>> pow(2,3,5)
    3
    >>>
    

      2.所有的参数必须是数值类型,不能是其他类型。

    >>> pow(2,'2')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'str'
    >>>
    

      3.若果x,y 有一个浮点数,则结果将转换为浮点数。

    >>> pow(2,3.2)
    9.18958683997628
    >>> pow(2,3.0)
    8.0

      4.如果x,y 都是整数,则结果也是整数,如果 y 是负数,浮点数不能取摸,所以传入的参数 z 不能取摸。如果可选参数 z 传入了值,则x,y 必须为整数,且 y 不能 负数,

    >>> pow(2,-1)
    0.5
    >>> pow(2,-2)
    0.25
    >>> pow(2,-10)
    0.0009765625
    >>>
    >>> pow(2,-10,2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: pow() 2nd argument cannot be negative when 3rd argument specified
  • 相关阅读:
    《C语言程序设计》指针篇<一>
    《算法竞赛入门经典》刘汝佳 C语言部分(前四章)“注解与习题” 之思索 -<1>
    程序设计第二次作业<2>
    程序设计第二次作业<1>
    第一次面向对象程序设计作业-大一下学期的自我目标
    android部署tensorflow
    vim操作
    跑groud truth的disparity
    tensorflow与android编译
    Ubuntu ndk环境变量配置
  • 原文地址:https://www.cnblogs.com/lincappu/p/8144320.html
Copyright © 2011-2022 走看看