associativity of operators

S

subramanian100in

What does "associativity of operators" mean ?

I am unable to undersatand this from K & R 2nd edition. Kindly explain
with an example.

Thanks
 
R

Richard Heathfield

(e-mail address removed), India said:
What does "associativity of operators" mean ?

I am unable to undersatand this from K & R 2nd edition. Kindly explain
with an example.


Consider the following code fragment:

int x = 4;
int y = 5;
int z = 6;
int r = x - y - z;

printf("r is %d\n", r);

What would you expect to be printed?

Is it:

(a) -7
(b) 5
(c) something else

If you answered (a), try to work out why (b) is also plausible.
If you answered (b), try to work out why (a) is also plausible.
If you answered (c), come back when you're feeling better. :)

Which answer you get depends on the order in which the subtractions are
done. 4 - (5 - 6) is different to (4 - 5) - 6. Clearly, precedence
cannot answer this question, since subtraction obviously has the same
precedence as subtraction!

So we need another tool for discriminating between operators at the same
precedence level. That tool is 'associativity'. Left-to-right
associativity means "do the stuff on the left first, and use the
calculated result as input for the stuff on the right", and
right-to-left associativity means, of course, the opposite.
 
R

Richard Harter

(e-mail address removed), India said:



Consider the following code fragment:

int x = 4;
int y = 5;
int z = 6;
int r = x - y - z;

printf("r is %d\n", r);

What would you expect to be printed?

Is it:

(a) -7
(b) 5
(c) something else

If you answered (a), try to work out why (b) is also plausible.
If you answered (b), try to work out why (a) is also plausible.
If you answered (c), come back when you're feeling better. :)

Which answer you get depends on the order in which the subtractions are
done. 4 - (5 - 6) is different to (4 - 5) - 6. Clearly, precedence
cannot answer this question, since subtraction obviously has the same
precedence as subtraction!

So we need another tool for discriminating between operators at the same
precedence level. That tool is 'associativity'. Left-to-right
associativity means "do the stuff on the left first, and use the
calculated result as input for the stuff on the right", and
right-to-left associativity means, of course, the opposite.

Nicely said. However it should be pointed out that in Mathematics
"associative" ordinarily means that it doesn't matter, i.e, if o is an
operator and X, Y, Z are elements, then o is associative if

(X o Y) o Z = X o (Y o Z)

* and + are associative; - and / are not. Left-to-right associativity
and right-to-left associativity are conventions for resolving
unparenthesized expressions.
 
M

Malcolm McLean

Richard Harter said:
Nicely said. However it should be pointed out that in Mathematics
"associative" ordinarily means that it doesn't matter, i.e, if o is an
operator and X, Y, Z are elements, then o is associative if

(X o Y) o Z = X o (Y o Z)

* and + are associative; - and / are not. Left-to-right associativity
and right-to-left associativity are conventions for resolving
unparenthesized expressions.
And associativity isn't just about notational conventions. It is actually
interesting to mathematicians.
 
C

christian.bau

Nicely said. However it should be pointed out that in Mathematics
"associative" ordinarily means that it doesn't matter, i.e, if o is an
operator and X, Y, Z are elements, then o is associative if

(X o Y) o Z = X o (Y o Z)

* and + are associative; - and / are not. Left-to-right associativity
and right-to-left associativity are conventions for resolving
unparenthesized expressions.

In mathematics, + and * are associative. Especially in floating-point
arithmetic, they are not (take x = 1e100, y = -1e100, and z = 1, then
(x + y) + z is not the same as x + (y + z). In C, + and * are left
associative, so x + y + z is (x + y) + z.
 
C

CBFalconer

christian.bau said:
.... snip ...

In mathematics, + and * are associative. Especially in floating-
point arithmetic, they are not (take x = 1e100, y = -1e100, and
z = 1, then (x + y) + z is not the same as x + (y + z). In C, +
and * are left associative, so x + y + z is (x + y) + z.

Not "in mathematics". For a single example, consider vector
multiplication. You need to specify the fields involved, rings,
etc.
 
D

Dik T. Winter

>
> Not "in mathematics". For a single example, consider vector
> multiplication. You need to specify the fields involved, rings,
> etc.

In mathematics those are generally not considered standard multiplications.
(Inner product and outer product in 3D, and in higher dimension vectors you
get into tensors when doing outer products.) But '*' is also not associative
in the sedenions. '*' *is* associative in a ring.
 
R

Richard Harter

In mathematics those are generally not considered standard multiplications.
(Inner product and outer product in 3D, and in higher dimension vectors you
get into tensors when doing outer products.) But '*' is also not associative
in the sedenions. '*' *is* associative in a ring.

Likewise the octonians.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top