26
$\begingroup$

I would like to know if there is any reason why many programming languages use the notation % for the modulo operator?

It is used in the most "famous" languages:

  • C
  • C++
  • C#
  • Go
  • Java
  • Julia
  • Lua
  • Perl
  • Python
$\endgroup$
6
  • 17
    $\begingroup$ The symbol $%$ looks like the division $/$ and it is only one character long. $\endgroup$
    – plop
    Commented Dec 16, 2020 at 11:25
  • 2
    $\begingroup$ Interesting question, I don't know. @plop is that only a guess? $\endgroup$
    – Juho
    Commented Dec 16, 2020 at 12:17
  • 3
    $\begingroup$ is javascript not famous? $\endgroup$
    – Rainb
    Commented Dec 18, 2020 at 8:00
  • 2
    $\begingroup$ @Rainb I think that you are confusing famous and infamous. $\endgroup$ Commented Dec 18, 2020 at 18:16
  • 4
    $\begingroup$ Is this a computer science question? Surely something like "retrocomputing" would be a better place for the history and development of notation. $\endgroup$
    – James K
    Commented Dec 18, 2020 at 22:28

3 Answers 3

45
$\begingroup$

The earliest known use of % for modulo was in B, which was the progenitor of C, which was the ancestor (or at least godparent) of most languages that do the same, hence the operator's ubiquity.

Why did Thompson and Richie pick %? It had to be a printable ASCII character that wouldn't conflict with B's other features. % was available, and it resembles the / division operator, making it the obvious choice.

p.s. the creator of ASCII invented \ to represent "reverse division", so it wasn't a candidate for modulo.

$\endgroup$
6
  • 3
    $\begingroup$ what is a reverse division? multiplication? $\endgroup$
    – user13267
    Commented Dec 17, 2020 at 5:46
  • 7
    $\begingroup$ a \ b = b / a. You can find this usage in sage for matrices, likely borrowed from somewhere else. $\endgroup$ Commented Dec 17, 2020 at 8:20
  • 1
    $\begingroup$ Note: for matrices in Matlab a\b isn't normally written b / a - it's normally written as (a)^1*b, which is different as matrix multiplication isn't commutative. $\endgroup$ Commented Dec 17, 2020 at 16:58
  • 2
    $\begingroup$ Is there any direct evidence that Bemer intended `\` to represent reverse division? The page here does not mention division at all. It seems he just wanted a character with a diagonal line. $\endgroup$ Commented Dec 18, 2020 at 13:56
  • 2
    $\begingroup$ According to citeseerx.ist.psu.edu/viewdoc/… Bemer wrote it in the June 1960 issue of Computing Reviews by ACM. A handful of academic libraries have physical copies, but there doesn't appear to be a digital version. $\endgroup$
    – Foo Bar
    Commented Dec 18, 2020 at 23:15
13
$\begingroup$

This is very likely a historical development. Looking at this table, we see that C was likely the first language to use % for modulo. Its predecesor BCPL used rem, and older languages such as Fortran, Algol, Lisp, and Cobol did not use %. But that's just my uninformed guess.

$\endgroup$
4
  • 5
    $\begingroup$ The "B" language which "C" replaced did also use it. See en.wikipedia.org/wiki/B_(programming_language). Looking at the manual (linked from that page), it says explicitly "The operator % denotes modulo." This strongly suggests Ken Thompson or Dennis Ritchie as the originator of the convention, since they designed B. UNIX shell script also uses % for modulo, although it's possible that scripting was not advanced enough to need it back in the early 70s. $\endgroup$
    – Graham
    Commented Dec 16, 2020 at 15:46
  • $\begingroup$ Very good. So BCPL didn't have it, but already B did. Wasn't B rather short-lived? $\endgroup$ Commented Dec 16, 2020 at 15:53
  • 7
    $\begingroup$ Yes, it was invented for B, then copied to C. Over half of the languages on the list above are direct descendants of C. And the others are either indirect descendants, or (like many languages) just copied a lot of their operators from C-descendant languages. $\endgroup$ Commented Dec 16, 2020 at 16:59
  • $\begingroup$ It seems to be pretty common for the answer to "Why does C do X?" to be "Because of B". $\endgroup$
    – Barmar
    Commented Dec 17, 2020 at 15:40
2
$\begingroup$

I would like to know if there is any reason why many programming languages use the notation % for the modulo operator?

I would strongly suspect it is because both Unix and Windows (the two operating system families that have survived to the present day), chose C as their main programming language. The result is most of the programming languages we use today, were designed in environments where C and/or C++ were the dominant "systems" programming language.

People designing a new language will undoubtedly have aspects that they wish to change from previous languages, but there will also be aspects that they think are fine the way they are or at least aspects they can't think of a better solution for. Development of mainstream programming languages has been more evolutionary than revolutionary.

The result has been that most successful programming languages over the past few decades have been directly or indirectly influenced by C. Lets look at the languages on your list in chronological order and see what environments they were developed in and/or what languages were used to develop them.

  • C++: designed by Bjarne Stroustrup while working on Unix at AT&T in 1979 as an object orientated extension of C.
  • Perl: Created by Larry Wall in 1987 while working at Unisys. Perl is written in C and it's clear from the initial announcement newsgroup post that perl was designed to be used on a Unix system.
  • Python: Created by Guido van Rossum around 1989 while working at a university. Again Python is written in C and was written with Unix, Windows, Mac and an experimental OS the university was working on in mind.
  • Lua: Created at a Brazilian University in 1993. It was written in C, though there were certainly many influences from other languages. It's probably the least C like of the languages here.
  • Java: designed by Sun Microsystems (a Unix vendor) in 1995. Largely implemented in C++ and borrows much of it's basic syntax from there.
  • C#: designed by Microsoft in 2000 as a substitute for Java after they fell out with Sun over MS-specific extensions to Java.
  • GO: Designed at Google in 2007 in reaction to problems Google engineers percived in C, C++ and Python, borrows most of it's syntax from C.
  • Julia: Developed in 2009, targetting Linux, Windows and Macos, appears that the non-julia parts are written in C.
$\endgroup$
4
  • $\begingroup$ Should mention Pascal - which uses the % operator, but unlike the others on the list does not appear to have its roots in C, coming from the ALGOL family instead. (I don't believe ALGOL used %, but there are many varieties of it, so maybe some do?) $\endgroup$ Commented Dec 17, 2020 at 17:38
  • 1
    $\begingroup$ Umm, I don't know what variant of pascal you are using, but the ones I'm familiar with don't use % for modulo, they use it as a prefix for binary numbers. $\endgroup$ Commented Dec 17, 2020 at 17:50
  • $\begingroup$ Hmm, might be a modern variant? I just looked it up at this site $\endgroup$ Commented Dec 17, 2020 at 19:09
  • 2
    $\begingroup$ @DarrelHoffman Looks like it might just be a mistake: in the precedence table at the bottom of the page it lists *, /, div, mod, and, & suggesting that mod not % is the modulo operator. $\endgroup$
    – IMSoP
    Commented Dec 18, 2020 at 14:41

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