By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

basic question on *=

P: 84
I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)...
Anyway, if i want to write a=b*(c+d) and write the following code:
Expand|Select|Wrap|Line Numbers
  1. a=b;
  2. a*=c+d;
does the program what I want or does it see a=b*c+d?
I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way
Jun 11 '07 #1
Share this Question
Share on Google+
9 Replies


Savage
Expert 100+
P: 1,764
I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)...
Anyway, if i want to write a=b*(c+d) and write the following code:
Expand|Select|Wrap|Line Numbers
  1. a=b;
  2. a*=c+d;
does the program what I want or does it see a=b*c+d?
I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way
It's same as a=a*(c+d) and becaue of a=b it is same as a=b*(c+d);

Savage
Jun 11 '07 #2

100+
P: 208
I'm now since a couple of weeks debugging a program and am now at the point where I don't know anymore what could be wrong (actually it compiles but does not do what I want)...
Anyway, if i want to write a=b*(c+d) and write the following code:
Expand|Select|Wrap|Line Numbers
  1. a=b;
  2. a*=c+d;
does the program what I want or does it see a=b*c+d?
I really hope it is the latter possibility, because that risks to solve my problem... thanks in advance either way
Expand|Select|Wrap|Line Numbers
  1. a=b;
  2. a*=c+d;
Does what you want it a = b * (c + d)
If you want it to be more readable you could just assign c+d to another variable and then do a *= var; That would produce the same results but maybe be a little more easy to read.

PS Savage beat me to it.
Jun 11 '07 #3

P: 84
It's same as a=a*(c+d) and becaue of a=b it is same as a=b*(c+d);

Savage
So a*=c+d is equivalent to a*=(c+d) ?

EDIT: and you beat me to my answer to savage :)

Thanks very much both of you.
Jun 11 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
a=b;
a*=c+d;
is equivalent to:
Expand|Select|Wrap|Line Numbers
  1. a = b*(c+d);
  2.  
This is a precedence issue. The *= operator has a precedence of 16 whereas addition has a precedence of 6. That means the addition is done before the *=.

Personally, I like your original method:

Expand|Select|Wrap|Line Numbers
  1. a = b*(c+d);
  2.  
because it's easier to read.
Jun 11 '07 #5

P: 23
Just thought that this precedence list

http://www.cppreference.com/operator_precedence.html

might help to justify why a*=c+d is equivalent to a=a*(b+c)

--Sorower
Jun 11 '07 #6

P: 84
T

Personally, I like your original method:

Expand|Select|Wrap|Line Numbers
  1. a = b*(c+d);
  2.  
because it's easier to read.
I agree that it easier to read, but if the b,c,d terms are very long (about one line) and the program is huge (260 MB of code) and you haven't written the program just modified it, which means that you might not know all the variables' names defined elsewhere and inherited to where you are, makes it difficult and risky to define for every b,c,d term a variables. So it leaves you no choice.
I would though never do it if it would be easily avoidable...

Also thanks about the explication of why via the precedences.
Jun 11 '07 #7

P: 84
Just thought that this precedence list

http://www.cppreference.com/operator_precedence.html

might help to justify why a*=c+d is equivalent to a=a*(b+c)

--Sorower

Thanks, such a list clears up also some other things, bookm arked it right away.
Jun 11 '07 #8

Savage
Expert 100+
P: 1,764
Expand|Select|Wrap|Line Numbers
  1. a=b;
  2. a*=c+d;
Does what you want it a = b * (c + d)
If you want it to be more readable you could just assign c+d to another variable and then do a *= var; That would produce the same results but maybe be a little more easy to read.

PS Savage beat me to it.
Muhahahahahah

Savage
Jun 11 '07 #9

Savage
Expert 100+
P: 1,764
So a*=c+d is equivalent to a*=(c+d) ?

EDIT: and you beat me to my answer to savage :)

Thanks very much both of you.
We are more than happy to help u.

Savage
Jun 11 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.