Public Group

#### Archived

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

# Module!?

This topic is 5764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

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

##### Share on other sites
How do you get that? I get 11/3= 3.666 forever. Now....how do you get 2 as a remainder??

Moose

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

##### Share on other sites
Alright...thanx. Sounds pretty useless so i''ll just try to forget about it.

Moose

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

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

##### Share on other sites
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".)

##### Share on other sites
hope you never want to make a random number

##### Share on other sites
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!

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633420
• Total Posts
3011790
• ### Who's Online (See full list)

There are no registered users currently online

×