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

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

问题描述

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

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

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