Advertisement

What does % (modulus) do?

Started by April 29, 2000 10:21 AM
10 comments, last by drago 24 years, 8 months ago
I would like to know what function the % operator has? I mean, what does it do? ---------- Drago
osu!
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
Advertisement
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**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
i will explain modulus (%)
look 10 % 3 = 1
ok....3*3 = 9, 10-9 = 1
1 is the answer
Heh, yeah, and also, you do realize that a modulus operation can only evaluate to an integer, no floating point numbers, hehe .
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
Visit our homepage: www.rarebyte.de.stGA
Advertisement
But you can''t do that in C or C++, modulus is only defined for integer values
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.

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

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.



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

GO LEAFS GO!
3D Math- The type of mathematics that'll put hair on your chest!

This topic is closed to new replies.

Advertisement