什么是运算符的结合性以及为什么它很重要?

发布于 2024-07-22 19:39:40 字数 30 浏览 7 评论 0原文

对于运算符来说什么是结合性以及为什么它很重要?

What is associativity for an operator and why is it important?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

人疚 2024-07-29 19:39:41

简单!!

Left Associative means we evaluate our expression from left to right

Right Associative means we evaluate our expression from right to left 

我们知道 *、/ 和 % 具有相同的优先级,但根据结合性,答案可能会改变:

例如:我们有表达式:4 * 8 / 2 % 5

Left associative:   (4 * 8) / 2 % 5 ==> (32 / 2) % 5 ==> 16 % 5 ==> 1

Right associative:  4 * 8 /(2 % 5) ==>  4 * ( 8 / 2) ==> 4 * 4 ==> 16

Simple!!

Left Associative means we evaluate our expression from left to right

Right Associative means we evaluate our expression from right to left 

We know *, /, and % have same precedence, but as per associativity, answer may change:

For eg: We have expression: 4 * 8 / 2 % 5

Left associative:   (4 * 8) / 2 % 5 ==> (32 / 2) % 5 ==> 16 % 5 ==> 1

Right associative:  4 * 8 /(2 % 5) ==>  4 * ( 8 / 2) ==> 4 * 4 ==> 16
瀟灑尐姊 2024-07-29 19:39:41

它是相同优先级的运算符的求值顺序。 从左到右或从右到左的顺序很重要。 因为

3 - 2 - 1

如果它是从左到右,那么它是

(3 - 2) - 1

并且是0。如果它是从右到左,那么它是

3 - (2 - 1)

并且它是2。在大多数语言中,我们说减号运算符具有从左到右的结合性。

2020 年更新:

如果声明是“当然我们是从左到右做的”,那么关于 3 - 2 - 1 的情况可能看起来微不足道。 但在其他情况下,例如在 Ruby 或 NodeJS 中完成:

$ irb
2.6.3 :001 > 2 ** 3 ** 2
 => 512 

** 是“to the power of”运算符。 结合性是从右到左。 它

 2 ** (3 ** 2)

2 ** 9,即 512,而不是

(2 ** 3) ** 2

8 ** 2,即 64

it is the order of evaluate for operators of the same precedence. The LEFT TO RIGHT or RIGHT TO LEFT order matters. For

3 - 2 - 1

if it is LEFT to RIGHT, then it is

(3 - 2) - 1

and is 0. If it is RIGHT to LEFT, then it is

3 - (2 - 1)

and it is 2. In most languages, we say that the minus operator has a LEFT TO RIGHT associativity.

Update 2020:

The situation about 3 - 2 - 1 might seem trivial, if the claim is, "of course we do it from left to right". But in other cases, such as if done in Ruby or in NodeJS:

$ irb
2.6.3 :001 > 2 ** 3 ** 2
 => 512 

The ** is "to the power of" operator. The associativity is from right to left. And it is

 2 ** (3 ** 2)

which is 2 ** 9, i.e., 512, instead of

(2 ** 3) ** 2

which is 8 ** 2, i.e., 64.

没有你我更好 2024-07-29 19:39:41

如果您指的是“运算符关联性” - 它是一种语言如何确定在没有括号的情况下如何对相同优先级的运算符进行分组。

例如,基于 C 的语言中的 + 和 - 运算符具有相同的优先级。 当您编写同时使用它们(不带括号)的表达式时,编译器必须确定计算它们的顺序。

如果您编写 12 - 5 + 3,则可能的计算包括:

  1. (12 - 5) + 3 = 10
  2. 12 - (5 + 3) = 4

根据计算表达式的顺序,您可以获得不同的结果。 在基于 C 的语言中,+ 和 - 具有左结合性,这意味着上面的表达式将作为第一种情况进行计算。

所有语言都有严格定义的优先级和结合性规则。 您可以在此处详细了解 C# 的规则。 运算符结合性优先级在维基百科上有很好的介绍。

If you are referring to "operator associativity" - it is how a language determines how operators of the same precedence are grouped in the absence of parentheses.

For example, the + and - operators in C-based languages have the same precedence. When you write an expression that uses both of them (without parentheses) the compiler must determine what order to evaluate them in.

If you write 12 - 5 + 3, the possible evaluations include:

  1. (12 - 5) + 3 = 10
  2. 12 - (5 + 3) = 4

Depending on the order you evaluate the expression in, you can get different results. In C-based languages, + and - have left associativity, which means that the expression above would evaluate as the first case.

All language have strongly-defined rules for both precedence and associativity. You can learn more about the rules for C# here. The general concepts of operator associativity and precedence are well covered on wikipedia.

半衾梦 2024-07-29 19:39:41

这是操作数与运算符的绑定顺序。 基本上:

a - b + c

可能被评估为(假设 - 和 + 具有相同的优先级):

((a - b) + c) 或,
(a - (b + c))

如果运算符是左关联的(立即绑定到左操作数),它将被评估为第一个。 如果它们是右结合的,则将其计算为第二个。

It's the order of binding of operands to an operator. Basically:

a - b + c

might be evaluated as (assuming - and + have the same precedence):

((a - b) + c) or,
(a - (b + c))

If operators are left associative (bind immediately to the left operand), it'll be evaluated as the first. If they are right associative, it'll be evaluated as the second.

離人涙 2024-07-29 19:39:41

如果您指的是运算符结合性:

它定义了解析表达式的方式。 它给出了一个标准,因此每个表达式都以相同的方式进行解析。

这对于具有相同优先级的操作来说非常重要,因为这可能会产生副作用。

If you mean operator associativity:

It defines the way expressions are parsed. It gives a standard, so every expression is parsed the same way.

It's mostly important for operations which have the same precedense, when there could be side effects.

梦在深巷 2024-07-29 19:39:41

前面的大多数示例都使用了常量。 如果参数碰巧是函数调用,则调用的顺序可能由关联规则确定,当然取决于您的编译器。 如果这些功能有副作用..

Most of the previous examples have used constants. If the arguments happen to be function calls, the order that the calls are made in may be determined by the association rules, depending of course on your compiler. And if those functions have side effects ..

初见你 2024-07-29 19:39:41

我们都知道优先级很重要,但解释表达式含义时的关联性也很重要。 如需真正简单的介绍,请尝试操作员的力量

We all know that precedence is important but so is associativity in interpreting the meaning of an expression. For a really simple intro try Power of Operators.

⊕婉儿 2024-07-29 19:39:41

关联性属于编程语言概念中的计算顺序。 计算的顺序决定了表达式的含义。 它有两个主要规则,

  1. 优先规则
  2. 关联规则

优先级规则定义不同类型的“相邻”运算符的计算顺序。 每种编程语言都有自己的关于其运算符的运算符优先级表。

回到结合性,

它定义了具有相同优先级的相邻操作的执行顺序。
它有3种口味,

左结合性
右结合性
非关联性

如果一个运算符是左结合的,那么它会从左到右进行计算,同样,如果它是右结合的,它会从右到左进行计算。

Associativity comes under the order of computation in the programming language concepts. The order of computation determines the meaning of the expression. It has two main rules,

  1. Precedence rules
  2. Associativity rules

precedence rules define the order in which "adjacent" operators of different types are evaluated. Every programming language has its own operator precedence table regarding its operators.

Coming back to the associativity,

It defines the order of execution of adjacent operations with the same precedence.
It has 3 flavors,

left-associativity
right-associativity
non-associativity

If an operator is left-associative it evaluates from left to right likewise if it is right-associative it evaluates from right to left.

唐婉 2024-07-29 19:39:40

对于运算符来说,关联性意味着当同一运算符出现在一行中时,我们首先应用哪个运算符出现。 在下面,让 Q 为运算符

a Q b Q c

如果 Q 是左结合的,那么它的计算结果为

(a Q b) Q c

如果它是右结合的,那么它的计算结果为

a Q (b Q c)

这很重要,因为它会改变表达式的含义。 考虑带有整数算术的除法运算符,它是左关联的

4 / 2 / 3    <=>    (4 / 2) / 3    <=> 2 / 3     = 0

如果它是右关联的,它将计算为未定义的表达式,因为您将除以零

4 / 2 / 3    <=>    4 / (2 / 3)    <=> 4 / 0     = undefined

For operators, associativity means that when the same operator appears in a row, then which operator occurence we apply first. In the following, let Q be the operator

a Q b Q c

If Q is left associative, then it evaluates as

(a Q b) Q c

And if it is right associative, then it evaluates as

a Q (b Q c)

It's important, since it changes the meaning of an expression. Consider the division operator with integer arithmetic, which is left associative

4 / 2 / 3    <=>    (4 / 2) / 3    <=> 2 / 3     = 0

If it were right associative, it would evaluate to an undefined expression, since you would divide by zero

4 / 2 / 3    <=>    4 / (2 / 3)    <=> 4 / 0     = undefined
冷弦 2024-07-29 19:39:40

结合律分为三种:

数学中的结合律

编程语言中的运算顺序

CPU 缓存中的关联性。

数学中的关联性是加法 (+) 等运算符的属性。 此属性允许您在不更改语句值的情况下重新排列括号,即:

(a + b) + c = a + (b + c)

在编程语言中,运算符的结合性(或固定性)是确定相同优先级的运算符如何在缺席中分组的属性。 /em> 括号; 即每个运算符的计算顺序。 这在编程语言之间可能有所不同。

在 CPU 缓存中,关联性是一种优化性能的方法。

There are three kinds of associativity:

The Associative property in mathematics

Order of Operations in programming languages

Associativity in CPU caches.

The Associative property in mathematics is a property of operators such as addition (+). This property allows you to rearrange parentheses without changing the value of a statement, i.e.:

(a + b) + c = a + (b + c)

In programming languages, the associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses; i.e. in what order each operator is evaluated. This can differ between programming languages.

In CPU caches, associativity is a method of optimizing performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文