为什么求幂从右到左? [英] Why is exponentiation applied right to left?

查看:124
本文介绍了为什么求幂从右到左?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读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:

在通常的计算机科学术语中,数学中的幂是右相关的,这意味着x y z 应该读作x (y z ),而不是(x y ) 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.

,并来自有关操作顺序 的维基百科:

and from 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).

So 2 ** 3 ** 4 is calculated as 2 ** (3 ** 4) (== 2417851639229258349412352) not (2 ** 3) ** 4 (== 4096).

这在编程语言中非常普遍;尽管有 例外,但它被称为 right-associativity (右关联).

This is pretty universal across programming languages; it is called right-associativity, although there are exceptions, with Excel and MATLAB being the most notable.

这篇关于为什么求幂从右到左?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆