|
|
coherent
modulus Definition modulus
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 opera-
tion is indicated with a percent sign `%'; therefore, 12 modulus
4 is written 1122%44.
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.
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 *****
definitions, operator
***** 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.
COHERENT Lexicon Page 1
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.