Jump to content
  • Advertisement

Archived

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

Nibbles

modulus, and what can it do?

This topic is 6149 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

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


Link to post
Share on other sites
Advertisement
ok, thanks

Scott


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

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

Share this post


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


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


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


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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!