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

  • Context: Undergrad 
  • Thread starter Thread starter swampwiz
  • Start date Start date
  • Tags Tags
    Operator
Click For Summary

Discussion Overview

The discussion revolves around the relationship between the modulo operator and remainders in both mathematics and programming. Participants explore definitions, interpretations, and the implications of using modulo in different contexts, including theoretical and practical applications.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants assert that the modulo operator always yields a result between 0 and the divisor minus 1, while others question this assumption when negative numbers are involved.
  • There is a discussion about the equivalence of "modulo" and "%" in programming, with some participants agreeing that they are synonymous, while others highlight differences in their mathematical and programming contexts.
  • One participant explains that in mathematics, "mod" is a binary relation, whereas in programming, "%" is a binary operator that returns a single number, leading to potential misunderstandings when transitioning between the two fields.
  • Some participants argue that while both mathematical mod and programming mod refer to the same underlying concept, they do not have the same semantics, particularly regarding how they handle equivalence classes and the representation of results.
  • There is a contention regarding the notation used for expressing congruences, with some suggesting that using "≡" is more appropriate than "=" in mathematical contexts.
  • Participants discuss the implications of how division and remainder operations can vary in programming languages, particularly regarding the sign of the remainder depending on how quotients are rounded.

Areas of Agreement / Disagreement

Participants express multiple competing views regarding the definitions and implications of modulo in mathematics versus programming. The discussion remains unresolved, with differing interpretations and no consensus reached.

Contextual Notes

Some limitations include the dependence on specific programming language implementations and the ambiguity surrounding the treatment of negative numbers in modulo operations. The discussion also highlights the need for clarity in notation and definitions when discussing mathematical concepts in programming contexts.

swampwiz
Messages
567
Reaction score
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:
 
Physics news on Phys.org
Yes, "modulo" and "%" are synonymous. The % operator is used in many computer programming languages.
 
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)
 
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.
 
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.
 
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?
 
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.
 
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.
 
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
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K