What is the Relationship Between Modulo and Remainders in Math and Programming?

  • Thread starter swampwiz
  • Start date
  • Tags
    Operator
In summary: Programming mod-n is a operator that...That's not really true. Both are operators in the sense that they are functions that take in two inputs and give one output. The only difference is that the programming mod-n operator is predefined and limited to integers, while the math mod-n operator can be defined for any type of numbers or objects. In summary, the conversation discusses the concept of modulo and its different uses in math and computer programming. In math, modulo is a binary relation that returns true/false for every pair of numbers, while in programming, it is a predefined operator that returns a single number. They both refer to the same underlying concept, but there are slight differences in their definitions and applications. Additionally, the conversation touches on the
  • #1
swampwiz
571
83
I am confused about this. I have always thought that the modulo operator always has the result of a while number between 0 and the modulo divisor minus 1.

I presume that the terms are called:

a % b = c

a : dividend

b : divisor

c : remainder

a & b > 0 : a % b = b * [ ( a / b ) - INT( a / b ) ]

23 % 4 = 4 * [ ( 23 / 4 ) - INT( 23 / 4 ) ] = 4 * [ { 5 + ( 3 / 4 ) } - INT( { 5 + ( 3 / 4 ) } ) ]

= 4 * [ { 5 + ( 3 / 4 ) } - { 5 } ] = 4 * ( 3 / 4 ) = 3

I am not so sure about the arguments not being positive, but I would think that the divisor must be a positive integer, and a negative dividend is simply added to by some product of the divisor that results in a positive number, which then goes through the modulo operation.

a < 0

d : positive integer

a % b = ( a + d b ) % b

d > INT( | a | / b )

-23 % 4 :

d > INT( | -23 | / 4 ) = INT( 23 / 4 ) = 5 → let d = 6

-23 % 4 = [ -23 + ( 6 ) ( 4 ) ] % 4 = 1 % 4 = 1

And thus there is the relationship that

a % b = { b - [ ( - a ) % b ] } % b

such that if

( a / b ) = INT ( a / b ) → a % b = 0

( a / b ) ≠ INT ( a / b ) → a % b = b - [ ( - a ) % b ]

Finally, I was reading an excerpt from the book Love and Math by Edward Frenkel, in which there is the statement

2 + 2 = 1 modulo 3

So presuming that modulo has the same meaning as %, and my earlier definition is accurate, this would mean

2 + 2 = 1 % 3 = 1 :confused:
 
Mathematics news on Phys.org
  • #2
Yes, "modulo" and "%" are synonymous. The % operator is used in many computer programming languages.
 
  • #3
swampwiz said:
So presuming that modulo has the same meaning as %, and my earlier definition is accurate, this would mean

2 + 2 = 1 % 3 = 1 :confused:
No, it means that if you apply the "modulo 3 operator" to both sides, you get equality.

Using your % notation,

2 + 2 = 1 modulo 3

means the same thing as

((2 + 2) % 3) = (1 % 3)
 
  • #4
Mod is different in math and computer programming. In math, mod is a binary relation:

a = b (mod n) if n divides a - b

So 5 = 11 (mod 2)

But in programming languages, % is a binary operator, not a relation. So

5 % 2 = 1 and 11 % 2 = 1.

That's because '%' is defined as you indicate ... a % n is the unique b such that a = b (mod n) and 0 <= b < n.

The math and programming mods refer to the same underlying concept; but they are not exactly the same thing. People going from math to programming or vice-versa need to be aware of the distinction.

Math mod-n is a relation that returns true/false for every pair of numbers. Programming mod-n is a operator that returns a single number, given a number and a modulus.
 
  • #5
SteveL27 said:
Mod is different in math and computer programming. In math, mod is a binary relation:

a = b (mod n) if n divides a - b

So 5 = 11 (mod 2)

But in programming languages, % is a binary operator, not a relation. So

5 % 2 = 1 and 11 % 2 = 1.

That's because '%' is defined as you indicate ... a % n is the unique b such that a = b (mod n) and 0 <= b < n.

The math and programming mods refer to the same underlying concept; but they are not exactly the same thing. People going from math to programming or vice-versa need to be aware of the distinction.

Math mod-n is a relation that returns true/false for every pair of numbers. Programming mod-n is a operator that returns a single number, given a number and a modulus.
I don't see the difference. Your example above, with 11 (mod 2) on one side, evaluates to 1. As an equation, you are bringing in another idea, that of equivalence classes. It probably should be written as 5 ##\equiv## 11 (mod 2), meaning that 5 and 11 are in the same equivalence class, modulo 2.
 
  • #6
Mark44 said:
I don't see the difference. Your example above, with 11 (mod 2) on one side, evaluates to 1. As an equation, you are bringing in another idea, that of equivalence classes. It probably should be written as 5 ##\equiv## 11 (mod 2), meaning that 5 and 11 are in the same equivalence class, modulo 2.

SteveL27 said:
So 5 = 11 (mod 2)

Didn't I say that?
 
  • #7
11 (mod 2) = 1
5 (mod 2) = 1
5 ≠ 11, but 5 ##\equiv## 11 (i.e., 5 and 11 are in the same equivalence class). That was my point.

Also, you said
SteveL27 said:
Math mod-n is a relation that returns true/false for every pair of numbers.
The way I see it, 11 (mod 2) and 11 % 2 both evaluate to 1, and are just two ways of saying the exact same thing.
 
  • #8
Mark44 said:
5 ≠ 11, but 5 ##\equiv## 11 (i.e., 5 and 11 are in the same equivalence class). That was my point.

Lazy markup on my part. I'll strive to do better. I've been writing 'a = b (mod n)' in ASCII for too long.
Mark44 said:
The way I see it, 11 (mod 2) and 11 % 2 both evaluate to 1, and are just two ways of saying the exact same thing.

I would disagree with you on that point. The OP's question (as I understood it) concerned the difference between the math mod and the programming mod.

If you had a programming language that returned 11 in response to the syntax '5 % 2' that would be one heck of a defective programming language.

It's true in math that 5 and 11 are in the same equivalence class mod 2. But in programming, the '%' operator chooses a particular representative from each equivalence class; namely the representative between 0 and n - 1 (inclusive) where n is the modulus.

I would submit that the mathematical mod relation and the programmer's % have different semantics. This is the question the OP was asking about. The math and programming notions of mod do not exactly coincide. 5 % 2 = 11 is the wrong output for a programming language; even though it expresses a mathematical truth.
 
  • #9
SteveL27 said:
Mod is different in math and computer programming. In math, mod is a binary relation ...
That's true when using mod in a congruence relation which normally uses ≡ not =. The modulo operation means the same thing in math as it does in programming when it refers to remainders or modular (finite field) math.

SteveL27 said:
negative numbers
In the case of programming languages division and remainder operations can vary depending if quotients are rounded towards zero (remainder has same sign as dividend) or rounded towards -∞ (remainder has same sign as divisor).

From a mathematical standpoint rouding towards -∞ is better because it's origin independent: if remainder = dividend % divisor, then remainder = (dividend ± (n * divisor)) % divisor, for any integer value of n.

Wiki articles:

http://en.wikipedia.org/wiki/Modular_arithmetic

http://en.wikipedia.org/wiki/Modulo_operation
 

Related to What is the Relationship Between Modulo and Remainders in Math and Programming?

1. What is the modulo operator?

The modulo operator, denoted as %, is a mathematical operation that returns the remainder after dividing one number by another.

2. How is the modulo operator used in programming?

In programming, the modulo operator is commonly used to determine if a number is even or odd, to loop through a set of numbers, or to generate a repeating pattern.

3. What is an example of using the modulo operator in code?

One example is using the modulo operator to determine if a number is even or odd. For instance, 5 % 2 would return 1 because 5 divided by 2 has a remainder of 1, indicating that 5 is an odd number.

4. Can negative numbers be used with the modulo operator?

Yes, negative numbers can be used with the modulo operator. The result will be the remainder after dividing the absolute values of the two numbers, with the sign of the dividend (first number).

5. Are there any limitations to using the modulo operator?

One limitation of the modulo operator is that it cannot be used when dividing by zero, as division by zero is undefined. Additionally, some programming languages may have different rules for handling negative numbers with the modulo operator.

Similar threads

Replies
11
Views
552
Replies
5
Views
2K
Replies
10
Views
475
Replies
6
Views
993
Replies
4
Views
955
Replies
4
Views
317
  • General Math
Replies
4
Views
764
Replies
1
Views
853
Replies
4
Views
737
Back
Top