By csguy

2019-04-14 00:35:55 8 Comments

From what I understand, in mathematics, the mod operator is the result of the remainder of Euclidean division. Where 0 ≤ r < |b|, meaning that the result will always be positive.

In programming however, there are operators in many languages which can be used to mean either the remainder operator or modulo operator which differ with respect to how they handle negative values.

(I believe that mod operator in math, remainder operator in programming, and mod operator in programming yield the same results for positive numbers)

According to Modulo operation with negative numbers

"With a remainder operator, the sign of the result is the same as the sign of the dividend while with a modulo operator the sign of the result is the same as the divisor."

So the mod operator in programming is not referring to the mod operator in math?

Is the sign of the answer the main distinguishing factor between mod operator vs remainder operator in programming?


@Bryce Wagner 2019-04-14 00:54:25

Mathematically, the modulus is part of group theory, and the idea of a set. You can generate all the numbers in a set by addition, with the modulus. So if your set is integers modulus 10, you count 0-9 and then start over at 0. There is no concept of a negative number under modulus.

In programming, the remainder is what portion is left after doing a division. So if you divide 3 by 10, you're left with 0 and a remainder of 3. If you divide -3 by 10, you get 0 with a remainder of -3, rather than -1 remainder 7. But the mathematical modulus is 7.

Those who designed integer division that we now use decided that it's more logical to round towards 0, rather than round towards negative infinity, so by necessity, a negative division will result in a negative remainder.

If you want to convert a remainder to a modulus, you need to add your modulus to any negative remainders in order to map them into the proper range.

@csguy 2019-04-14 01:33:15

so although remainder and mod in programming are different (remainder being what is left after division, mod being finding "wrap around numbers") they are both calculated in a similar fashion which involves finding the remainder?

@Bryce Wagner 2019-04-14 02:06:33

Yes, they're identical for positive numbers, so that's probably why sometimes remainder is called "mod" operator.

Related Questions

Sponsored Content

4 Answered Questions

6 Answered Questions

[SOLVED] Modulo for negative dividends in Python

  • 2012-04-08 14:28:26
  • canyon289
  • 6739 View
  • 8 Score
  • 6 Answer
  • Tags:   python modulo

3 Answered Questions

[SOLVED] Why does C++ output negative numbers when using modulo?

  • 2012-07-24 11:54:52
  • Martin Thoma
  • 22413 View
  • 58 Score
  • 3 Answer
  • Tags:   c++ standards modulo

1 Answered Questions

[SOLVED] Javascript modulo operator behaves differently from other languages

1 Answered Questions

1 Answered Questions

[SOLVED] Modulo using negative numbers

  • 2015-04-24 11:49:40
  • Matt
  • 395 View
  • 1 Score
  • 1 Answer
  • Tags:   modulo modulus

2 Answered Questions

2 Answered Questions

[SOLVED] Python modulus the same as remainder?

  • 2013-10-10 01:15:40
  • George Newton
  • 2689 View
  • 4 Score
  • 2 Answer
  • Tags:   python modulo

8 Answered Questions

[SOLVED] Mathematical modulus in c#

  • 2010-04-22 13:10:49
  • penguat
  • 21913 View
  • 28 Score
  • 8 Answer
  • Tags:   c# modulo

Sponsored Content