COHERENT manpages
This page displays the COHERENT manpage for modulus [Definition].
List of available manpages
Index
modulus -- Definition
Modulus is the operation that returns the remainder of a division
operation. For example, 12 modulus four equals zero, because when 12 is
divided by four it leaves no remainder. The term ``modulo'' also refers to
the product of a modulus operation; in the above example, the modulo is
zero. In C, the modulus operation is indicated with a percent sign `%';
therefore, 12 modulus 4 is written 12%4.
The modulus operation often is used to trim numbers to a preset range. For
example, if you wanted to create a list of single-digit random numbers, you
would use the command:
rand()%10
This is demonstrated by the following example.
Example
This example prints a list of 20 single-digit random numbers. The random-
number table is seeded with a portion of the current system time.
#include <stdio.h>
#include <stdlib.h>
main()
{
long nowhere; /* place to put unused data */
int counter;
srand((int)time(&nowhere));
for (counter = 0; counter <20; counter++)
printf("%d\n", rand()%10);
}
See Also
operator,
Programming COHERENT
Notes
The implementation of C defines how a modulus operator behaves when it
operates upon numbers with different signs. On the i8086,
10 % -4
yields -2. This is not mathematical modulus, which is +2.