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?

### Related Questions

#### Sponsored Content

#### 4 Answered Questions

### [SOLVED] C remainder/modulo operator definition for positive arguments

**2019-11-08 14:12:11****towi****77**View**4**Score**4**Answer- Tags: c division modulo integer-division

#### 6 Answered Questions

#### 3 Answered Questions

#### 1 Answered Questions

### [SOLVED] Javascript modulo operator behaves differently from other languages

**2016-11-19 10:53:27****Uri Goren****345**View**0**Score**1**Answer- Tags: javascript division modulo

#### 1 Answered Questions

### [SOLVED] Why does the modulo operator behave differently from mathematical faction MOD

**2016-08-31 11:36:03****hecate****154**View**0**Score**1**Answer- Tags: c# math division modular-arithmetic

#### 1 Answered Questions

#### 2 Answered Questions

### [SOLVED] Java - Is there a method for Euclidean or floored modulo

**2013-02-08 09:58:17****boumbh****3517**View**12**Score**2**Answer- Tags: java operators modulo modulus negative-number

## 1 comments

## @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.