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?

6 Answers 6


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.

  • 4
    +1 C's influence on pretty much every non-LISP language since the early 1970s cannot be overstated. Commented Nov 9, 2013 at 14:45

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.

  • 3
    For that reason, Haskell has two operators: rem for the remainder and mod for modulus with the properties you describe.
    – Ingo
    Commented Jul 24, 2013 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
    Commented Jan 17, 2014 at 0:30

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 ℕ

  • ℤ are the integers (and ℕ is a subset of integers), you must mean from ℝ to ℤ.
    – Joni
    Commented Jul 23, 2013 at 23:54
  • @Joni: Mixed two examples, fixed.
    – MSalters
    Commented Jul 23, 2013 at 23:59

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!

  • 1
    This answer makes no sense... What is the connection between using "mod" and using different units???
    – user317163
    Commented Oct 9, 2018 at 9:38
  • 1
    How many yard and feet in 75 inches?
    – gnasher729
    Commented Mar 25, 2023 at 14:22

The modulo operation is essential in many algorithms. I give you an amount of money in pennies, you want to split it into dollars and pennies. Very easy using the modulo operator. To you it might not be a fundamental operation. To me, it is.

  • How often are you actually given an amount of money in pennies, that you want to split into dollars and pennies?
    – Steve
    Commented Mar 25, 2023 at 13:19
  • That is one example. Print a number with thousands separator. If 40 kids fit into a bus, and there are 112 kids, what do you do? Display 19 images, up to 3 per row.
    – gnasher729
    Commented Mar 25, 2023 at 14:22
  • I was just remarking, wryly, that the example you've provided isn't so common as to make it obvious why the mod operator is considered a fundamental one! I've used a few of the major accounting systems and most use a decimal fixed-point type, or a floating-point type (I know!). I've only seen one that used a plain integer type for money (hence, counting everything in pennies), and that was a bespoke system for a specialist sector.
    – Steve
    Commented Mar 25, 2023 at 17:52

The modulo operator is essential to modular arithmetic - that is, arithmetic with integers. The common applications of this are calculations with money and calculations with dates.

It's perhaps with the advance of the metric system that the special arithmetic regimes that apply to money and dates are not taught in schools anymore - or at least not prominently.

The British £sd (or LSD) imperial system of money was a mixed radix system, consisting of pounds, shillings, and pence. There were 12 pence to a shilling, and 20 shillings to a pound. A penny could be further divided into half-pennies (1/2), farthings (1/4), or half-farthings (1/8, I think only existing briefly in the poorest colonies).

The Gregorian system of dates owes its fundamental tenets to the medieval Julian system, which is a mixed radix system expressing dates as years, months, and days, but (unlike imperial money in this respect...) with an irregular and varying number of days per month - 12 months per year, and 28-31 days per month.

Handling money and dates are amongst the most fundamental uses of electronic computers, and so the existence of a division/remainder operator to properly handle arithmetic with integers could certainly be expected as part of the fundamental set of instructions.

In a computing context, modular arithmetic also describes the behaviour of most integers under overflow, and can also be useful for implementing things like circular buffers (although if the buffer is the size of a power of 2, the modulo operator can be implemented by using the AND operator to mask off higher bits).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.