What is associativity of operators and why is it important?

試著忘記壹切 提交于 2019-12-17 08:05:29

问题


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

Updated: operator associativity


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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 / 0) ==> Undefined Behaviour

I hope this would help.




回答6:


I assume you mean operator associativity...

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.




回答7:


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.




回答8:


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 ..




回答9:


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.



来源:https://stackoverflow.com/questions/930486/what-is-associativity-of-operators-and-why-is-it-important

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!