#### Archived

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

# modulus, and what can it do?

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

## Recommended Posts

Hi, I know that modulus will display the remainder of a division but i''m not sure how to use it. Can I use it to display, for example, the 3 in 12.3? If not, how would I do that? thanks, Scott ------------------------------------------------------------ Email Website
"If you try and don''t succeed, destroy all evidence that you tried." ------------------------------------------------------------

##### Share on other sites
ok, thanks

Scott

------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------

Try x-(int)x

##### Share on other sites
Perhaps the best way to appreciate the use of the modulus operator is to see its graph. If you were to graph y = x % k, where k is a constant, you would see a sawtooth pattern emerge, with both a period and an amplitude of k. It would look something like this:

     /|     /|     /|    / |    / |    / |   /  |   /  |   /  |  /   |  /   |  /   | /    | /    | /    |/     |/     |/     |

Now, for some applications...

It is 92830272938367 seconds since the very beginning of the year 0. If you were to divide this into individual years, days, hours, minutes, and seconds, and write them down in the format YY:DD:HH:MM:SS, what would the value of the "SS" part be?

92830272938367 % 60

You have a map of height values for terrain in a game, and you want it to tile infinitely. The map''s dimensions are 512 x 256. The player is at (1028, 9823), and you want to find what his z (height) coordinate should be. You know that it will be whatever value is in the heightmap underneath him. What entry in the heightmap should you look up?

The height value at (1028 % 512, 9823 % 256)

I hope that helps.

##### Share on other sites
Re. fractional modulo..

If you''re using C or C++, you can use fmod(x, y) , from <math.h> to do a modulus calculation.

To find the fractional component, you can,as shalom said, use x - (int)x . If the numbers are too large to fit in an int you can use x - floor(x) , also in <math.h> .

If x is negative, you may need to reverse the sign when you get the fractional part and then reverse it back again, since the standard appears to be a little vague on what floor() does to negative numbers (floor(-0.5) might be -1.0 or -0.0, depending upon your interpretation of what makes a ''large'' number).

Many C libraries also provide an explicit ''fractional part'' function, but I can''t find one in the standard. The nearest I can get is fract = modf(x, &temp) - the integer portion of x is placed in the double pointed to by temp. It''s probably easier to use the subtraction method.

All your bases belong to us (I know. It''s irony.)

##### Share on other sites
thanks, I ended up doing something like x-(int)x.

Scott

------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------

##### Share on other sites
I wrote an Isometric tileenginge a while back and i used
the MOD operator to find out if a tile was odd or even
in the Y axis, since odd tiles needed to be shifted to the
right about half a tile, so they would tile correctly.

PS: Did that make any sense?

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013559
×

## Important Information

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!