#### Archived

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

# What does % (modulus) do?

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

## Recommended Posts

I would like to know what function the % operator has? I mean, what does it do? ---------- Drago

##### Share on other sites
You remeber how you learned about remainers in 3rd grade. Well, % does a division, but instead of evaluating to the amount of times argument2 goes into argument1, it does the division and takes the remainder instead. So:

10 % 3 = 1

12 % 11 = 1

27 % 12 = 3

##### Share on other sites
The only think I could add to that, is that % is called the modulus operator ;-)

#pragma DWIM // Do What I Mean!
**I use Software Mode**

##### Share on other sites
i will explain modulus (%)
look 10 % 3 = 1
ok....3*3 = 9, 10-9 = 1

##### Share on other sites
Heh, yeah, and also, you do realize that a modulus operation can only evaluate to an integer, no floating point numbers, hehe .

##### Share on other sites
Zipster, you could also extend the modulus to floating point values:
e.g.
10.3 % 1.5 = 1.3
because 6*1.5 + 1.3 = 10.3
but instead of 6 you are only allowed to take integer values.

GA

##### Share on other sites
But you can''t do that in C or C++, modulus is only defined for integer values

##### Share on other sites
As for the use of modulus, it''s very good for giving a number a bound. For example, if you''re given something that counts to 100, and you need it to count from 1 to 4, you can mod it by 4, and it''ll then count from 0 to 3. Add one and it''ll count from 1 to 4, as you need.

You''ll see it quite often in calculating random numbers. The C rand() function returns a 32-bit int. If you need a smaller number (and you almost always do), you can mod it to make it smaller.

For example, rand()%100 will give you a value from 0 to 99.

##### Share on other sites
quote:
For example, rand()%100 will give you a value from 0 to 99.

0 to 99 INCLUSIVE, just wanted to make that clear, hehe.

##### Share on other sites
I''m blonde, fifteen and too lazy to look in a dictionary, but, what does inclusive mean?

GO LEAFS GO!

• 10
• 10
• 12
• 21
• 33