What is happening here?

Started by
14 comments, last by Mscgamer 20 years, 10 months ago
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...
Advertisement
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.
that''s strange. When I tested this out with borland''s C++5.5 compiler I get 0 as quotient and -2 as remainder...

--{You fight like a dairy farmer!}

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...
I AM an Army of One... I just have 10,000 other Armies of One to back me up!
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. )

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.

This topic is closed to new replies.

Advertisement