What is happening here?
Ok, I am learning from the C++ for Dummies book by Cogswell. In the section about variables he talks about adding, subtracting, multiplying and dividing them. The dividing part is a bit confusing...
Here is the part that confuses me:
int first = 30;
first /= 5;
cout << first << endl;
//In this case, first becomes 6, because 30/5 is 6.
*I understand this*
int first = 33;
first %= 5;
cout << first << endl;
//In this case, first becomes 3, because the remainder of 33 divided by 6 is 3.
*I do not understand this*
Ok... that''s weird. I thought he was dividing 33 by 5... where did he get 33 divided by 6? Was it just a typo or something?
Yes, that is a typo, his book is known to have them, particularly with his explanation of the modulus operator.
[edited by - BioagentX on May 28, 2003 8:06:27 PM]
[edited by - BioagentX on May 28, 2003 8:06:27 PM]
quote:Original post by bioagentX
Yes, that is a typo, his book is known to have them, particularly with his explanation of the modulus operator.
[edited by - BioagentX on May 28, 2003 8:06:27 PM]
Thanks.
After I finish Cogswell''s book I''m planning on getting C++ Primer Plus. I would have gotten it first, but Cogswell''s book was the only one at my local book store.
quote:Original post by Mscgamer
Ok, I am learning from the C++ for Dummies book by Cogswell. In the section about variables he talks about adding, subtracting, multiplying and dividing them. The dividing part is a bit confusing...
Here is the part that confuses me:
int first = 30;
first /= 5;
cout << first << endl;
//In this case, first becomes 6, because 30/5 is 6.
*I understand this*
int first = 33;
first %= 5;
cout << first << endl;
//In this case, first becomes 3, because the remainder of 33 divided by 6 is 3.
*I do not understand this*
Ok... that''s weird. I thought he was dividing 33 by 5... where did he get 33 divided by 6? Was it just a typo or something?
No it is not a typo, it says :
first %= 5
"%" is a modulus operator. It returns the value which is the remainder of the division problem...
To divide just do /...
BattleGuard
Whenever I try to find a better signature than this... Well, I can''t... This is it, Sorry...
quote:Original post by Mscgamer
int first = 33;
first %= 5;
cout << first << endl;
//In this case, first becomes 3, because the remainder of 33 divided by 6 is 3.
*I do not understand this*
Ok... that''s weird. I thought he was dividing 33 by 5... where did he get 33 divided by 6? Was it just a typo or something?
Yes, that 6 in the comment is a typo.
The other way to think about the modulus operator is ''keep subtracting 5 from first until it is less than 5, and give me the result.''
Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
quote:Original post by superpig
The other way to think about the modulus operator is ''keep subtracting 5 from first until it is less than 5, and give me the result.''
How does C (or C++) do modulus for negative numbers? Is it undefined? implementation defined? defined to be between (to use the example above) 0 (inclusive) and 5 (exclusive)?
Ok, hang on. It depends on how you look at it. The 6 could easily NOT be a typo. Modulus is tricky (especially with negative numbers, ill get to that in a sec).
So that statement expands to x = 33 % 5;
So, how I like to explain this to people is it''s a numberline, and the result is the distance from the closest multiple of 5 to the value you''re mod''ing. So, in this case, the result is the distance between the closest multiple of 5 to 33 (which is 30), and the value 33 itself. That distance is 3. The reason he says the remainder of 33 / 6 is 3, is because that''s what it is. The remainder of 33 / 5 is also 3. That''s because that closest multiple to 33 that we spoke of is 30, which is 5 * 6. Whether you say 33 / 6, or 33 / 5, is really moot in this *specific* case. Of course, it''s conceptually easier (I think) to say 33 / 5, but 33 / 6 is just as correct.
As to explaining modulus, this is the picture I like.
So if you have the problem Y % X, you would draw the number line like this...
0 x 2x 3x 4x
<---|---|---|---|---|--->
And then find where your Y is. Then, you would find the distance from the closest multiple of X (that is BELOW your Y value) and count up to it.
Now, as for negative numbers. I''m not a huge fan of negative numbers and modulus in programming.
Mathematically (afaik, I''m not super math guru guy), the modulus is ALWAYS positive. Thus, the -2 % 5 would be 3. However, I know that VC6 compiler defines A % B based on A / B. In other words, A % B = A - (A / B) * B
What this means, is that -2 % 5 actually returns -2. This makes me sad. I''m not sure if this is C++ specific. I don''t think it is, since the VC6 documentation i was looking at at the time implied that this was the VC6 implementation (in other words it didn''t state it was the C standard). I could look that up, I guess.
Any ways, 33 % 6 and 33 % 5 are both 3. So it doesn''t matter how you say it. 5 * 6 = 30. I guess it depends on how you think of modulus. 33 % 5... well... let''s see... 5 goes into 33 6 times, so 33 / 6 gives us a remainder of 3. -OR- 33 % 5... well... let''s see... 33 / 5 gives us a remainder of 3. I personally think the second is more intuitive, but maybe the author didn''t. Doesn''t make it wrong, just a different viewpoint. Although you''d think it clarify the modulus operator more than with just that little comment.
Sorry, modulus can get me going sometimes.
So that statement expands to x = 33 % 5;
So, how I like to explain this to people is it''s a numberline, and the result is the distance from the closest multiple of 5 to the value you''re mod''ing. So, in this case, the result is the distance between the closest multiple of 5 to 33 (which is 30), and the value 33 itself. That distance is 3. The reason he says the remainder of 33 / 6 is 3, is because that''s what it is. The remainder of 33 / 5 is also 3. That''s because that closest multiple to 33 that we spoke of is 30, which is 5 * 6. Whether you say 33 / 6, or 33 / 5, is really moot in this *specific* case. Of course, it''s conceptually easier (I think) to say 33 / 5, but 33 / 6 is just as correct.
As to explaining modulus, this is the picture I like.
So if you have the problem Y % X, you would draw the number line like this...
0 x 2x 3x 4x
<---|---|---|---|---|--->
And then find where your Y is. Then, you would find the distance from the closest multiple of X (that is BELOW your Y value) and count up to it.
Now, as for negative numbers. I''m not a huge fan of negative numbers and modulus in programming.
Mathematically (afaik, I''m not super math guru guy), the modulus is ALWAYS positive. Thus, the -2 % 5 would be 3. However, I know that VC6 compiler defines A % B based on A / B. In other words, A % B = A - (A / B) * B
What this means, is that -2 % 5 actually returns -2. This makes me sad. I''m not sure if this is C++ specific. I don''t think it is, since the VC6 documentation i was looking at at the time implied that this was the VC6 implementation (in other words it didn''t state it was the C standard). I could look that up, I guess.
Any ways, 33 % 6 and 33 % 5 are both 3. So it doesn''t matter how you say it. 5 * 6 = 30. I guess it depends on how you think of modulus. 33 % 5... well... let''s see... 5 goes into 33 6 times, so 33 / 6 gives us a remainder of 3. -OR- 33 % 5... well... let''s see... 33 / 5 gives us a remainder of 3. I personally think the second is more intuitive, but maybe the author didn''t. Doesn''t make it wrong, just a different viewpoint. Although you''d think it clarify the modulus operator more than with just that little comment.
Sorry, modulus can get me going sometimes.
quote:Original post by Tibre
Ok, hang on. It depends on how you look at it. The 6 could easily NOT be a typo. Modulus is tricky (especially with negative numbers, ill get to that in a sec).
So that statement expands to x = 33 % 5;
So, how I like to explain this to people is it''s a numberline, and the result is the distance from the closest multiple of 5 to the value you''re mod''ing. So, in this case, the result is the distance between the closest multiple of 5 to 33 (which is 30), and the value 33 itself. That distance is 3. The reason he says the remainder of 33 / 6 is 3, is because that''s what it is. The remainder of 33 / 5 is also 3. That''s because that closest multiple to 33 that we spoke of is 30, which is 5 * 6. Whether you say 33 / 6, or 33 / 5, is really moot in this *specific* case. Of course, it''s conceptually easier (I think) to say 33 / 5, but 33 / 6 is just as correct.
As to explaining modulus, this is the picture I like.
So if you have the problem Y % X, you would draw the number line like this...
0 x 2x 3x 4x
<---|---|---|---|---|--->
And then find where your Y is. Then, you would find the distance from the closest multiple of X (that is BELOW your Y value) and count up to it.
Now, as for negative numbers. I''m not a huge fan of negative numbers and modulus in programming.
Mathematically (afaik, I''m not super math guru guy), the modulus is ALWAYS positive. Thus, the -2 % 5 would be 3. However, I know that VC6 compiler defines A % B based on A / B. In other words, A % B = A - (A / B) * B
What this means, is that -2 % 5 actually returns -2. This makes me sad. I''m not sure if this is C++ specific. I don''t think it is, since the VC6 documentation i was looking at at the time implied that this was the VC6 implementation (in other words it didn''t state it was the C standard). I could look that up, I guess.
Any ways, 33 % 6 and 33 % 5 are both 3. So it doesn''t matter how you say it. 5 * 6 = 30. I guess it depends on how you think of modulus. 33 % 5... well... let''s see... 5 goes into 33 6 times, so 33 / 6 gives us a remainder of 3. -OR- 33 % 5... well... let''s see... 33 / 5 gives us a remainder of 3. I personally think the second is more intuitive, but maybe the author didn''t. Doesn''t make it wrong, just a different viewpoint. Although you''d think it clarify the modulus operator more than with just that little comment.
Sorry, modulus can get me going sometimes.
Wow, thanks. Exactly what I was looking for.
Still one thing that I need to know, though:
int first = 33;
first %= 5;
cout << first << endl;
//In this case, first becomes 3, because the remainder of 33 divided by 6 is 3.
Was the 6 in his comment a typo? When you do first %= 5; isn''t the computer just dividing 33 by 5 and finding the remainder? He said that he was finding the remainder in 33 by 6, but in the actual problem there is no 6, but a 5.
I see how dividing 33 by 6 or 5 both produces the remainder 33 (6 * 5 = 30. 33 - 30 = 3). His comment led me to believe that the modulus operator did something else besides just divide 33 by 5. If it was just a typo I completely understand how to use the modulus operator in the way he showed, if not I guess I am still confused.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement