Modulus, What is it?

Started by
5 comments, last by Way Walker 18 years, 2 months ago
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?
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



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.


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



Okay, thanks guys. It was bugging me all night at work.
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.

Stephen M. Webb
Professional Free Software Developer

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)

This topic is closed to new replies.

Advertisement