Jump to content
  • Advertisement

Archived

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

drago

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
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 this post


Link to post
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.

Visit our homepage: www.rarebyte.de.st

GA

Share this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


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

GO LEAFS GO!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!