Jump to content
  • Advertisement
Sign in to follow this  
guitarguy310

Modulus, What is it?

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

What exactly is Modulus and how does one calculate it? I know it's the remainder of a division of numbers, but where did it come from and why don't we use it everyday with our other four arithmetic operators?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by guitarguy310
What exactly is Modulus and how does one calculate it? I know it's the remainder of a division of numbers, but where did it come from and why don't we use it everyday with our other four arithmetic operators?


http://en.wikipedia.org/wiki/Modulus

Share this post


Link to post
Share on other sites
Guest Anonymous Poster



You can do am integer divide and take the result and multiply it by the divisor and subtract from the original number to get the modulus.

I forget if there are Assembler codes on certain CPUs that give the modulus directly.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


You use it when you need the remainder (much less commonly needed....)


The most I seem to use it in C is when generating random numbers in a simulation

as the Rand() returns a value 0..32767 and to change it to a value 0..7 you do:

INT num = Rand() % 8; // returns reasonably random 0..7


another Ive used in the past was calculating the size of a partial buffer

numfullbuffers = datasize / buffersize;
partialbufbytes = datasize % buffersize; // if 0 then no last 'partial' buffer



Share this post


Link to post
Share on other sites
Quote:
Original post by guitarguy310
What exactly is Modulus and how does one calculate it? I know it's the remainder of a division of numbers, but where did it come from and why don't we use it everyday with our other four arithmetic operators?


It's used every day if you deal with random number generation or cryptography. You also use it when dealing with clock time, dates, and other cyclical values (exactly what happens to your clock at 63 minutes after 11:00 PM?).

There's an entire branch of mathematics that deals with modulus. It, along with rotational symmetry, was the impetus for algebraic theory (groups, rings, fields, quaternions, etc etc etc).

The C, and of course C++, language does not provide a modulus operator. It provides the '%' operator, which is similar as long as your dealing with the domain of positive integers. It usually gives the incorrect result for non-positive integers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
The C, and of course C++, language does not provide a modulus operator. It provides the '%' operator, which is similar as long as your dealing with the domain of positive integers. It usually gives the incorrect result for non-positive integers.


Actually, the problem isn't that the results are incorrect, but that the C89 standard didn't specify which convention to use when working with negative numbers. Other languages provide multiple functions (e.g. modulus and remainder functions) so you can choose the behavior. C99 (and maybe C++?) finally made a decision, and I think it was in favor of allowing negative results.

On a related note, it's somewhat common to use the overflow of unsigned integer types to calculate a modulus. A common example (around here) is using an unsigned char to store angles. In this case you have UCHAR_MAX degrees, and you get the wrap around ("modulus") for free. (The standard defines overflow as the number modulo UTYPE_MAX+1)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!