问题描述
我正在阅读 Python 简介教科书并遇到了这一行:
I am reading an Intro to Python textbook and came across this line:
同一行上的运算符具有相同的优先级,并且从左到右应用,但取幂除外,它从右到左应用.
Operators on the same row have equal precedence and are applied left to right, except for exponentiation, which is applied right to left.
我了解其中的大部分内容,但我不明白为什么他们说取幂是从右到左应用的.他们也没有提供任何示例.另外,我可以问这样的一般性问题,还是只喜欢解决问题的问题?
I understand most of this, but I do not understand why they say exponentiation is applied right to left. They do not provide any examples either. Also, am I allowed to ask general questions like this, or are only problem solving questions preferred?
推荐答案
**
运算符遵循 正常的数学约定;它是右结合的:
The **
operator follows normal mathematical conventions; it is right-associative:
在通常的计算机科学术语中,数学中的取幂是右结合的,这意味着 xyz 应该读作 x(yz),而不是 (xy)z.在对 BODMAS 规则的阐述中,对这个问题的处理非常谨慎,规则是首先评估最高指数.
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.
来自维基百科关于操作顺序:
如果用堆叠符号表示求幂,通常的规则是从上到下工作,因为求幂在数学中是右结合的.
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).
So 2 ** 3 ** 4
is calculated as 2 ** (3 ** 4)
(== 2417851639229258349412352) not (2 ** 3) ** 4
(== 4096).
这在编程语言中非常普遍;它被称为右关联性,尽管有个例外,其中 Excel 和 MATLAB 是最值得注意的.
This is pretty universal across programming languages; it is called right-associativity, although there are exceptions, with Excel and MATLAB being the most notable.
这篇关于为什么取幂从右到左?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!