Not Yet Answered # associativity of operators

TotoTitus 6

-1

I'm not sure if this is the place for associativy discussion - this is more a math thing :D

So, here it goes:

Let's analyze the addition (+) operation.

2+3=5. We all know that.

2+3+5 = 10. Again, we all know that.

I'll put some redundant parentheses:

(2+3)+5=10

but also

2+(3+5)=10

When adding numbers, this is extremely obvious, that's why i called the parentheses ' redundant '. It's obvious that it doesn't matter what i add first 2 and 3 or 3 and 5.

Same for multiplication:

2*3*4 = 24

(2*3)*4 = 24

2*(3*4) = 24.

We call addition and multiplication *associative* operations, because the order in which we operate the numbers *doesn't matter*

Let's take another operation, for instance, division:

8/4 = 2.

8/4/2 = 1. First we divide 8 by 4 ( and obtain 2) , and then we divide the resulting 2 by 2, and thus we get 1. (We execute operations from left to right)

But if we change the order of operations:

8/(4/2) = 4. First, because of the parentheses, we divide 4 by 2 (and get 2). THEN only, we divide 8 by 2 and get 4.

Same with substraction:

8-4-2 = 2 . First, we substract 4 from 8 (obtaining 4), and then we substract 2 from the previously obtained 4 -> bingo we obtain 2

But in the following situation:

8-(4-2) = 6. Because of the parentheses, we first substract 2 from 4 (=2), and then substract the previously obtained 2 from 8 -> 6.

So, we call substraction and division *non-associative* operations because the order in which we deal with the operations matters.

Edit: There's more:

Even non-associative operations can have a *form* of associativity.

8-4-2 is not equal to 8 - (4 -2 )

BUT

8-4-2 IS equal to (8- 4) -2, because we process the expression from left to right.

Such operations ( division included), in which forcing the evaluation to left-to-right using parentheses yields the same result as when parentheses are not used, are called *left-associative* operations.

Let's take another operation: Power.

2^3^2 = 512.

BUT

2^3^2 = 2^(3^2) = 2^9 = 64.

BUT

(2^3)^ 2 = 64 which is NOT equal to 2^3^2

In this example, putting parentheses to the left will change the result, whereas putting parentheses at the right of the expression will not change the result, because power is processed from RIGHT to LEFT.

Such operations, where forcing the evaluation of the expression from RIGHT to LEFT yields the same result as if parentheses were not used, are called*right-associative* operations.

An operation is associative when it is *both left-associative, and right-associative* (<=> order of evaluating doesn't matter), otherwise it is unassociative.

*Edited 7 Years Ago by TotoTitus*: n/a

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I am currently creating a simulation of a pizza ordering system in object oriented program. I have some question. the instruction and guideline is long but I will try and cut it down a lot. the instruction is to create a program that simulate a pizza restaurant using ...

```
#include <stdio.h>
int main(void)
int movieChoice;
int seatNum;
int seatPlan[5][10] = { 0 };
int rows, cols;
char space;
void seatDisplay(int a[][10]);
void seatNew(int n[][10]);
void seatDisplay(int a[][10]) { //this function shows the seat plan
for (int i = 0; i<5; i++) {
for (int j = 0; j<10; j++) ...
```

I have this tree format.

```
> myapp->
> setup.py
> tests ->
> test_myapp.py
> myapp ->
> __init__.py
> myapp.py
```

And from test_myapp.py, I am calling: `from myapp import myapp`

and when I run`python setup.py test`

I am getting `No module named 'myapp'`