• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# What is happening here?

## Recommended Posts

Mscgamer    286
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?

##### Share on other sites
bioagentX    130
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]

##### Share on other sites
Mscgamer    286
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.

##### Share on other sites
BattleGuard    122
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...

##### Share on other sites
superpig    1825
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.

##### Share on other sites
Way Walker    745
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)?

##### Share on other sites
Tibre    175
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.

##### Share on other sites
Mscgamer    286
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.

##### Share on other sites
stustill    122
Well, either the 6 in the comment, OR the 5 in the code is a typo!

Stu

##### Share on other sites
Mscgamer    286
quote:
Original post by stustill
Well, either the 6 in the comment, OR the 5 in the code is a typo!

Stu

That''s good and bad. Good that I understand and bad that there might be some less obvious typos later in the book.

##### Share on other sites
Guest Anonymous Poster
quote:
Original post by Tibre
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.

Wouldn''t A - (A / B) * B = A - A?

And when I work it out, -2 % 5 == -2, unless I''m doing something wrong?

-2 / 5 = 0 since 5 can not go into -2. Therefore, you have -2 left over. I don''t see that could possibly be 3? Though I''m probably wrong...

##### Share on other sites
Agape    122
quote:
Original post by Anonymous Poster
quote:
Original post by Tibre
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.

Wouldn''t A - (A / B) * B = A - A?

And when I work it out, -2 % 5 == -2, unless I''m doing something wrong?

-2 / 5 = 0 since 5 can not go into -2. Therefore, you have -2 left over. I don''t see that could possibly be 3? Though I''m probably wrong...

-2 == 3, in the set of numbers mod 5. Welcome to the fun that is abstract algebra.

##### Share on other sites
Greatwolf    125
that''s strange. When I tested this out with borland''s C++5.5 compiler I get 0 as quotient and -2 as remainder...

##### Share on other sites
ah_bk88    122
Somebody stop me if you have a better suggestion, but after you finish that book, I''d recommend Ivor Horton''s Beginning Visual C++. You don''t need to have any knowledge of the C or C++ language, but it does help to speed things along, and you get a great instruction to Visual C++. I haven''t read C++ Primer Plus, so I''m not saying it isn''t a good book, but I have read Mr. Horton''s book and can atest to it''s great quality.

Just Smile and Nod...

##### Share on other sites
Mscgamer    286
quote:
Original post by ah_bk88
Somebody stop me if you have a better suggestion, but after you finish that book, I''d recommend Ivor Horton''s Beginning Visual C++. You don''t need to have any knowledge of the C or C++ language, but it does help to speed things along, and you get a great instruction to Visual C++. I haven''t read C++ Primer Plus, so I''m not saying it isn''t a good book, but I have read Mr. Horton''s book and can atest to it''s great quality.

Just Smile and Nod...

Umm... what''s the difference between Visual C++ and normal C++ (or whatever you call what I''m learning right now. )

##### Share on other sites
Tibre    175
AP: In math terms, yes, A-(A/B)*B would equal A-A, or 0. But remember that (A/B)*B doesn''t always give you A on a computer, since the result would be truncated (assuming we''re dividing integers of course, and not floats... an important assumption that i left out).

So on many compilers, if A is negative and B is positive, A % B would also be negative.

However, in math terms, atleast as far as I recall, the modulus has to be a number in the range of [0, B). In other words, it always has to be positive. The reason -2 % 5 would be 3, is because (think of the number line I diagrammed... poorly) you go from the first multiple of B BELOW your A and add up to it. So the multiple of 5 you would use is -5, not 0. The difference between -5 and -2 is 3, thus -2 % 5 would be 3. You always go from some multiple of B UP to A, never down. This is in math terms, not the terms most compilers use. This actually caused me an annoying bug that really surprised me when I discovered it. It had never occured to me that the modulus operator wasn''t some standardized operator. Although maybe it is, just different from what I was used to it meaning.

Anyways, lesson learned. Never assume anything.