Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is there a reason, historical or otherwise, why the modulus operator is part of a small set of standard operators in what seems like many languages? (+, -, *, / and %, for Java and C, with ** in Ruby and Python).

It seems strange to include mod as a "fundamental" (not to knock it, I use it plenty, but I also use exponentiation, absolute value, floor/ceiling or others -- they seem just as useful and necessary). Was this an old decision made in some specification which Java, C, Ruby and Python all follow or a language they are all descended from? As far as I can tell most Lisp dialects only include +, -, / and *.

At first I wondered if mod was particularly easy to implement at the binary level (would that even make a difference, regarding decisions about what should be a "fundamental" operator and what shouldn't?) but it seems not to be. Is it just much more commonly used in programming than I think?

share|improve this question
add comment

migrated from stackoverflow.com Jul 28 '13 at 6:41

This question came from our site for professional and enthusiast programmers.

4 Answers

up vote 15 down vote accepted

I am sure it is common because many CPU architectures implement modulus as a second output of the integer divide instruction.

I don't recall it being present in 1970s CPUs (6800, 8080, Z80, 1604, etc.), but by the 1980s, the Intel 8086 and 8088, as well as the Motorola 6809 had it.

The PDP-11 instruction architecture specified DIV producing a quotient and a remainder from the beginning (1970), though the MUL and DIV instructions were not present on early designs, but could be transparently emulated by an "instruction not implemented trap" and implemented with a handler that did bit twiddling. Probably the PDP-11 feature encouraged the very first edition of the C language providing the % feature. (Ever notice how a percent sign has a slash in it? That makes it a cleverish choice for a division related operator.)

The presence of modulus in C alone can probably explain its presence in all modern languages. C has a very large family of descendants and was otherwise quite influential.

share|improve this answer
1  
+1 C's influence on pretty much every non-LISP language since the early 1970s cannot be overstated. –  Ross Patterson Nov 9 '13 at 14:45
add comment

Many programming languages have a "remainder" operator which may be used as a modulus operator when both operands are positive; said operator is often called the "modulus" operator, because that is its primary use. Languages generally have such an operator because many hardware platforms' division hardware automatically supply a remainder when performing a division, and computing a remainder or modulus via any other means would be much more difficult.

I don't know the history of hardware support for signed division; many processors have for years provided hardware that can automatically perform signed division subject to the rule that if a/b yields (q,r), then -a/b or a/-b will yield (-q,-r), but I'm not sure of the use cases where division using that rule is particularly helpful. In almost every case where I've used integer division or "modulus" operations on negative values, I've wanted round-toward-negative-infinity on the division and a true modulus operation (such that (a+b)/b would always equal (a/b)+1 and (a+b)%b would always equal a%b.). Because the operators don't work that way, it's necessary to test the sign of the dividend and use different code when it's negative--essentially negating any benefit from having a signed divide instruction in the first place. I'm curious for what purposes the signed-division support in hardware is actually useful.

Returning to the original question, the modulus operator is often useful in situations where certain things are supposed to happen on a periodic basis, either in space (e.g. graphical coordinates) or in time. For example, if one wants to have an event happen every 15 seconds, the time until the next event will be 15-((time_now - time_of_an_occurrence) % 15), assuming time_of_an_occurrence is not greater than time_now. If time_of_an_occurrence were greater than time_now, a modulus operator could continue to use the same formula provided the subtraction didn't overflow, but the remainder operator will require a different formula.

share|improve this answer
2  
+1 for calling it “remainder” –  Complicated see bio Jul 23 '13 at 23:39
1  
For that reason, Haskell has two operators: rem for the remainder and mod for modulus with the properties you describe. –  Ingo Jul 24 '13 at 23:28
    
@Complicatedseebio: What's particularly funny is that it's often called the modulus operator because it's generally used to compute the modulus, even when that requires code like m = number % base; if (m < 0) m+=base;. I don't know that I've ever seen any code that benefited from the remainder operator going negative, except perhaps q = n/d; if (n%d < 0) q+=1;, which could in any case be written better in other ways. –  supercat Jan 17 at 0:30
add comment

Modulus is closely related to group and ring theory, which are very fundamental mathematical theories.

Exponentiation is just the third operation in the sequence addition, multiplication, exponentiation, tetration (and that's an infinite sequence). It does become important mainly with complex numbers, which are rarer in computer arithmetic. One particular exponentiation is supported explicitly, though: 2n is commonly written as 1<<n, since computers are quite binary.

Floor and ceil are really rare in comparison: They only apply when converting from ℝ to ℤ. (floating point to integer). Similarly, abs is associated with a mapping from ℤ to ℕ

share|improve this answer
    
ℤ are the integers (and ℕ is a subset of integers), you must mean from ℝ to ℤ. –  Joni Jul 23 '13 at 23:54
    
@Joni: Mixed two examples, fixed. –  MSalters Jul 23 '13 at 23:59
add comment

Sorry, but at the risk of turning this into a game of "Call My Bluff" I think the real answer to this question is quite simple:

Mod allows precise computations in "non-decimal" quantities and units such as dates, time, yards, inches, ounces etc. In decimal calculations, it also provides a method for the programmer to work to a numeric precision beyond that provided by the hardware of the machine. This has a huge number of applications from the very small (e.g quantum calculations) to the very large (e.g. discovering new prime numbers).

It is important to understand that we called these things computers for a reason. Sometimes we need them to give us the correct answer!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.