17xie > Python 语言参考手册 > 5.4 幂运算符 The power operator
背景:                 
[本书目录] [图书首页] [本书讨论区]  
链接地址:http://www.17xie.com/read-37441.html    注册17xie 一起来写书 实现您的出书梦想!


5.4 幂运算符 The power operator

The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. The syntax is:

幂运算符比在操作数左边的一元运算符有更高的优先级; 但比右面的一元运算符要低.句法为:

power  ::=  primary ["**" u_expr]
Download entire grammar as text.

Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands).

因此, 在一串没有括号的由幂运算符和一元运算符组成的序列, 会从左到右面求值(没有强制改变求值顺序).

The power operator has the same semantics as the built-in pow() function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type. The result type is that of the arguments after coercion.

当以两个参数调用pow() 时, 幂运算符与内建函数 pow()有相同的语义: 生成左边参数的右边参数次方.数值型参数首先转换成通用类型. 结果类型是参数经强制规则转换后的结果; 若结果不能以该类型表达(计算整数的幂结果为负数, 或计算负浮点数的幂为无效的值), 则引发一个TypeError 异常.

With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long 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. (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised).

Raising 0.0 to a negative power results in a ZeroDivisionError. Raising a negative number to a fractional power results in a ValueError.



5.5 一元算术运算符 Unary arithmetic operations

All unary arithmetic (and bit-wise) operations have the same priority:

所有一元算术运算符(和位运算符)有相同的优先级:

u_expr  ::=  power | "-" u_expr | "+" u_expr | "~" u_expr
Download entire grammar as text.

The unary - (minus) operator yields the negation of its numeric argument.

一元运算符-(减)对其数值型操作数取负.

The unary + (plus) operator yields its numeric argument unchanged.

一元运算符+(加)不改变其数值型操作数.

The unary ~ (invert) operator yields the bit-wise inversion of its plain or long integer argument. The bit-wise inversion of x is defined as -(x+1). It only applies to integral numbers.

一元运算符 (取反)对其普通整数或长整数参数求逆(比特级).x的比特级求逆运算定义为-(x+1).它仅仅用于整数型的操作数.

In all three cases, if the argument does not have the proper type, a TypeError exception is raised.

在以上所有的三种情况下, 如果参数的类型不合法, 就会引发一个TypeError异常.


字数:2505    最后更新:8个月以前 [03-15 20:53]月落晨星 修改
本页编辑者:月落晨星  
[前一页]:5.3.3 片断 Slicings  [后一页]:5.6 二元算术运算符 B…
[在本页中加入书签] [收藏本书] [推荐本书]
  17xie论坛 > 本书讨论区 > 本页评论   (共0条)
发表评论

用户名称 匿名发表
评论内容
验证码

关于我们 | 版权声明 | 免责声明 | 诚聘英才 | 联系我们 | 合作伙伴 | 友情链接 | 广告合作 | 提交意见
Copyright © 2007 17xie.com 互联网协同写书平台 京ICP备08002671号