Advertisement

Module!?

Started by February 08, 2003 05:49 PM
8 comments, last by moose_2006 21 years, 9 months ago
What is module in C++? Like when you use the % sign? It says 11%3 is 2...but how the heck do you get 2? What exactly does % do?? Please help!! Moose
what you mean is modulus. all it does is give you the remainder when doing integer division. so 11 divided by 3 is 3, remainder 2.

cyn
Advertisement
How do you get that? I get 11/3= 3.666 forever. Now....how do you get 2 as a remainder??

Moose
Actually it''s pronounced "modulus" operator. It gives the remainder after dividing the smaller integer into the larger one. So instead of the float expression 6.0/4.0 returning 1.5, it returns how many times the 4.0 can go into the 6.0 as a whole integer, the remainder is what''s returned. In this case 2.0. Here are some examples:

11%2, 11/2=5, 5x2=10, 11-10=1, remainder: 1
9%4, 9/4=2, 2x4=8, 9-8=1, remainder: 1 again!!!(conincidence)
360%320, 360/320=1, 360-1*320=40, remainder: 40

In other words, it''s like doing division problems for the first time in 3rd grade--instead of having to find a rounded decimal(3.14159...), you''d find the integer part of how many times the denomenator goes into the numerator, but the answer is not the integer quotient, but the remainder. It''s just like when the teacher said, "just write down the remainder next to the answer."
The modulus operator takes its operands and returns the remainder of their integral quotient...?
This post will self-destruct in 5 seconds...Actually, it's just Benjamin Bunny doing his thing.
Alright...thanx. Sounds pretty useless so i''ll just try to forget about it.

Moose
11/3 is 3.666666..

Remember though, 0.66666... can be expressed as a fraction, 2/3.

If you were to reduce 11/3 into a proper fraction, 11/3 becomes 3 and 2/3.

If you''re doing integer division, you get 3. If you''re doing integer modular arithmetic, you get 2. Think of it like this, when you divide, you''re gonna get the whole number part of the proper fraction, when you do modular arithmetic, you get the remainder (whatever the numerator of the leftover fraction is).

Another way to look at modular arithmetic is to think about clocks (modular arithmetic is also called clock arithmetic). Here''s a link to an online tutorial about clock arithmetic.
http://www.math.csusb.edu/faculty/susan/number_bracelets/mod_arith.html

Hope this helps.
-TheQSource
Advertisement
quote:
Alright...thanx. Sounds pretty useless so i''ll just try to forget about it.

Not really... I mean maybe its not the most useful thing around but as you work with c(++) you very well may find it rears its head as a simple alternative many times.
quote: Original post by moose_2006
Sounds pretty useless so i''ll just try to forget about it.

I don''t mean to be rude, but that''s a very ignorant thing to say. It''s certainly irrelevant to floating point operations, but as soon as you deal with integer divisions, dividends and remainders are going to get fairly important. As just one example of where it is useful, consider the concept of divisibility: A number n is divisible by a number d (note that this concept is only applicable to integers, so let n and d be integers) iff n mod d equals 0, i.e. n%d == 0.

(No, I did not misspell the word "if".)
hope you never want to make a random number
-PoesRaven
Modulus is not useless.

For example, consider...

if (a == 4 || a == 8 || a == 12 || a == 16 /* etc */
|| a == 244) {}

you could just change that to

if ((a % 4) == 0) // if a is evenly divisible by 4...

(since 4 is a power of two, you could also change it to a & 3)

you can use it to keep a number in bounds...

unsigned int c = 42;
while (1) {
c = (c + 1) % 50;
}

keeps c within range 0-49.

And as said, for random numbers...

int a = rand() % 6;

and more! BASK IN THE GLORY OF MODULUS!

This topic is closed to new replies.

Advertisement