Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is operator% referred to as the "modulus" operator instead of the "remainder" operator?

Tags:

People also ask

What is the difference between modulus and remainder?

Modulo or Remainder Operator returns the remainder of the two numbers after division. If you are provided with two numbers, say A and B, A is the dividend and B is the divisor, A mod B is there a remainder of the division of A and B. Modulo operator is an arithmetical operator which is denoted by %.

Which operator is known as modulus operator?

The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division. produces the remainder when x is divided by y.

What is the purpose of the modulus operator?

The modulus operator is added in the arithmetic operators in C, and it works between two available operands. It divides the given numerator by the denominator to find a result. In simpler words, it produces a remainder for the integer division. Thus, the remainder is also always an integer number only.

How modulus operator is different from division operator?

Modulo − Represents as % operator. And gives the value of the remainder of an integer division. Division − represents as / operator. And gives the value of the quotient of a division.


Today at work I had an interesting discussion with one of my coworkers. He was surprised when he had the following happen to him:

assert(-1 % 10 == -1)  //Expecting 9 

So when he came to ask me about it, I told him "well, that makes sense. When you divide -1 by 10, you get 0 with -1 remaining. His argument however was that the modulus operator is supposed to hold true to the "always positive" model. I did a little research and found that the modulus he was referring to looks like this:

Let q be the integer quotient of a and n. Let r be the remainder. Then:

a = n * q + r

The definition I was using, however, appears to be the Knuth version of modulus, which is:

Let q be the floor of a divided by n. Let r be the remainder. Then:

r = a - n * q

So, my question is why it ended up in the FORTRAN standard (and subsequently the C-standard) to have the modulus operator truncate toward 0? It seems like a misnomer to me to call it "modulus" and not "remainder" (In math, the answer really should be 9). Is this related to how hardware is doing the division?

For reference:

  • Wikipedia on Modulus
  • MSDN entry on the "modulus" operator
    (Yes, i realize its for VS2003...I'm stuck with it currently. Sadface)
  • Modulus operator changes
  • Don't assume positive remainder...

TLDR; Is hardware the reason the modulus operator truncates toward 0?