为什么求幂从右到左?
Why is exponentiation applied right to left?
我正在阅读 Python 教科书的介绍,并遇到了这一行:
Operators on the same row have equal precedence and are applied left to right, except for exponentiation, which is applied right to left.
我大部分都明白,但我不明白为什么他们说求幂是从右到左应用的。他们也没有提供任何例子。另外,我可以问这样的一般性问题,还是只推荐解决问题的问题?
来自 http://docs.python.org/reference/expressions.html
同一框内的运算符从左到右分组(除了比较,包括测试,它们都具有相同的优先级和从左到右的链——参见比较部分——和求幂,从右到左分组)。
>>> 2 ** 2 ** 2
16
>>> 2 ** 2 ** 2 ** 2
65536
>>> (2 ** 2 ** 2) ** 2
256
对于中间情况 2 ** 2 ** 2 ** 2
,这是中间步骤 -
- 分解为
2 ** (2 ** (2 ** 2))
2 ** (2 ** (4)) # progressing right to left
2 ** (16) # this is 2 to the power 16
最终评估为 65536
希望对您有所帮助!
这个解释对我来说似乎很清楚。让我向您展示一个可能会启发您的示例:
print 2 ** 2 ** 3 # prints 256
如果你从左到右阅读这篇文章,你会先做 2 ** 2
,结果是 4,然后是 4 ** 3
,结果是 64。
看来我们的答案是错误的。 :)
但是,从右到左...
你会先做 2 ** 3
,这是 8,然后,2 ** 8
,给我们 256 !
我希望我能为你启发这一点。 :)
编辑:Martijn Pieters 更准确地回答了您的问题,抱歉。我忘了说这是数学约定。
**
运算符在normal mathematical conventions之后;它是右结合的:
In the usual computer science jargon, exponentiation in mathematics is right-associative, which means that xyz should be read as x(yz), not (xy)z. In expositions of the BODMAS rules that are careful enough to address this question, the rule is to evaluate the top exponent first.
来自 Wikipedia on the Order of Operations:
If exponentiation is indicated by stacked symbols, the usual rule is to work from the top down, because exponention is right-associative in mathematics.
因此 2 ** 3 ** 4
计算为 2 ** (3 ** 4)
(== 2417851639229258349412352) 而不是 (2 ** 3) ** 4
(== 4096)。
这在编程语言中非常普遍;它被称为 右结合性,尽管有 个例外,其中 Excel 和 MATLAB 最为显着。
我正在阅读 Python 教科书的介绍,并遇到了这一行:
Operators on the same row have equal precedence and are applied left to right, except for exponentiation, which is applied right to left.
我大部分都明白,但我不明白为什么他们说求幂是从右到左应用的。他们也没有提供任何例子。另外,我可以问这样的一般性问题,还是只推荐解决问题的问题?
来自 http://docs.python.org/reference/expressions.html
同一框内的运算符从左到右分组(除了比较,包括测试,它们都具有相同的优先级和从左到右的链——参见比较部分——和求幂,从右到左分组)。
>>> 2 ** 2 ** 2
16
>>> 2 ** 2 ** 2 ** 2
65536
>>> (2 ** 2 ** 2) ** 2
256
对于中间情况 2 ** 2 ** 2 ** 2
,这是中间步骤 -
- 分解为
2 ** (2 ** (2 ** 2))
2 ** (2 ** (4)) # progressing right to left
2 ** (16) # this is 2 to the power 16
最终评估为65536
希望对您有所帮助!
这个解释对我来说似乎很清楚。让我向您展示一个可能会启发您的示例:
print 2 ** 2 ** 3 # prints 256
如果你从左到右阅读这篇文章,你会先做 2 ** 2
,结果是 4,然后是 4 ** 3
,结果是 64。
看来我们的答案是错误的。 :)
但是,从右到左...
你会先做 2 ** 3
,这是 8,然后,2 ** 8
,给我们 256 !
我希望我能为你启发这一点。 :)
编辑:Martijn Pieters 更准确地回答了您的问题,抱歉。我忘了说这是数学约定。
**
运算符在normal mathematical conventions之后;它是右结合的:
In the usual computer science jargon, exponentiation in mathematics is right-associative, which means that xyz should be read as x(yz), not (xy)z. In expositions of the BODMAS rules that are careful enough to address this question, the rule is to evaluate the top exponent first.
来自 Wikipedia on the Order of Operations:
If exponentiation is indicated by stacked symbols, the usual rule is to work from the top down, because exponention is right-associative in mathematics.
因此 2 ** 3 ** 4
计算为 2 ** (3 ** 4)
(== 2417851639229258349412352) 而不是 (2 ** 3) ** 4
(== 4096)。
这在编程语言中非常普遍;它被称为 右结合性,尽管有 个例外,其中 Excel 和 MATLAB 最为显着。