17xie > Python 语言参考手册 > 5.6 二元算术运算符 Binary arithmetic operations
背景:                 
[本书目录] [图书首页] [本书讨论区]  
链接地址:http://www.17xie.com/read-37443.html    注册17xie 一起来写书 实现您的出书梦想!


5.6 二元算术运算符 Binary arithmetic operations

The binary arithmetic operations have the conventional priority levels. Note that some of these operations also apply to certain non-numeric types. Apart from the power operator, there are only two levels, one for multiplicative operators and one for additive operators:

二元算术运算符的优先级符合我们的正常习惯.注意其中有些运算符也可以应用于非数值型操作数.除了幂运算符, 它们只分两个优先级: 一个是乘法类运算,一个是加法类运算.

Download entire grammar as text.

The * (multiplication) operator yields the product of its arguments. The arguments must either both be numbers, or one argument must be an integer (plain or long) and the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; a negative repetition factor yields an empty sequence.

*(乘)运算符计算其操作数的积, 其两个参数必须是数值型的, 或一个是整数(普通或长整数)另一个是有序类型.第一种情况下, 数值参数被转换成通用类型然后计算积. 后一种情况下, 重复连接有序类型对象. 一个负连接因子产生一个有空的有序类型对象.

The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the `floor' function applied to the result. Division by zero raises the ZeroDivisionError exception.

/(除)运算符生成参数的商.数值型参数首先转换成通用类型,普通整数或长整数的除法计算结果是相同类型整数,结果就是对商的精确结果执行floor()函数的返回在值.除以零会引发ZeroDivisionError 异常.

The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand5.1.

%(取模)计算第一个参数除以第二参数得到的余数.数值型参数首先转换成通用类型. 右面的参数为零, 会引发ZeroDivisionError异常. 参数可以浮点数,例如 3.14 The integer division and modulo operators are connected by the following identity: x == (x/y)*y + (x%y). Integer division and modulo are also connected with the built-in function divmod(): divmod(x, y) == (x/y, x%y). These identities don't hold for floating point numbers; there similar identities hold approximately where x/y is replaced by floor(x/y) or floor(x/y) - 15.2.

整除和取模运算可以用以下等式联系起来: x == (x/y)*y + (x

Deprecated since release 2.3. The floor division operator, the modulo operator, and the divmod() function are no longer defined for complex numbers. Instead, convert to a floating point number using the abs() function if appropriate.

The + (addition) operator yields the sum of its arguments. The arguments must either both be numbers or both sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated.

+(加)运算符计算参数的和.参数必须都是数值型的, 或都是相同有序类型的对象. 对于前一种情况, 它们先转换成通用类型, 然后相加. 后一种情况下, 所有有序类型对象被连接起来

The - (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type.

-(减)计算参数的差, 数值型的参数首先转换成通用类型.



注脚

... operand5.1
While abs(x%y) < abs(y) is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double-precision number, in order that -1e-100 % 1e100 have the same sign as 1e100, the computed result is -1e-100 + 1e100, which is numerically exactly equal to 1e100. Function fmod() in the math module returns a result whose sign matches the sign of the first argument instead, and so returns -1e-100 in this case. Which approach is more appropriate depends on the application.
... 15.2
If x is very close to an exact integer multiple of y, it's possible for floor(x/y) to be one larger than (x-x%y)/y due to rounding. In such cases, Python returns the latter result, in order to preserve that divmod(x,y)[0] * y + x % y be very close to x.


5.7 移位运算符 Shifting operations

The shifting operations have lower priority than the arithmetic operations:

移位运算符的优先级比算术运算符低.

shift_expr  ::=  a_expr | shift_expr ( "«" | "»" ) a_expr
Download entire grammar as text.

These operators accept plain or long integers as arguments. The arguments are converted to a common type. They shift the first argument to the left or right by the number of bits given by the second argument.

这些运算符接受普通整数和长整数作为参数. 参数都被转换通用类型.它们将第一个参数向左或向右移动第二个参数指出的位数.

A right shift by n bits is defined as division by pow(2,n). A left shift by n bits is defined as multiplication with pow(2,n); for plain integers there is no overflow check so in that case the operation drops bits and flips the sign if the result is not less than pow(2,31) in absolute value. Negative shift counts raise a ValueError exception.

右移n位可以定义为除以pow(2,n),左移n位可以定义为乘以pow(2,n); 对于普通整数是没有溢出检查的,因此若结果的绝对值不小于pow(2,31), 这个运算会截掉相应的位并且符号位也在移位处理之列.


字数:5274    最后更新:8个月以前 [03-15 20:54]月落晨星 修改
本页编辑者:月落晨星  
[前一页]:5.4 幂运算符 The pow…  [后一页]:5.8 二元位运算符 Bin…
[在本页中加入书签] [收藏本书] [推荐本书]
  17xie论坛 > 本书讨论区 > 本页评论   (共0条)
发表评论

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

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