I Modulo and Modulus in math and computer programming

AI Thread Summary
The discussion clarifies the distinction between the modulo operator (%) in programming and the concept of modulus in mathematics. The modulo operator provides the remainder of integer division, while modulus defines a range of integers from 0 to N-1, with numbers outside this range wrapping around. In programming, the modulo operation can also yield a congruent number within the defined modulus range. Different programming languages handle the modulo operation and its behavior with negative numbers and non-integer values in various ways. Understanding these differences is crucial for accurate implementation in coding.
fog37
Messages
1,566
Reaction score
108
TL;DR Summary
Understand if Modulo and Modulus in math and computer programming are related and how...
Hello,

I an clear on how the modulo operator ##%##, also called the remainder operator, is used in programming. It gives the remainder of the integer division. For example, ##5 % 2 = 1## because the divisor 2 fits 2 wholly times into the dividend 5 leaving behind a reminder of 1.
## 6% 3 =0## because the remainder is 0.

However, when learning about modular arithmetic in math, I learned that modulus ##N## means that only the integer numbers from ##0## to ##N-1## are available. For example, modulus ##5## means that all the possible numbers we can work with are ##0,1,2,3,4##. Any other number ##X## outside this range wraps up back into this range of numbers as a congruent number to one of numbers in the range.
A simple hack is to add or subtract from the number ##X## an integer number of the modulus ##N## until we get back into the range ##0,1,2,3,4##. For example, 10 (modulus 5) is congruent to 10-(2-5)=0 (modulus 5).

Example: ##10## and ##0## are equivalent/congruent numbers if considered modulus 5: $$ 10 \equiv 0 mod(5)$$. There are two ways to assess if two numbers are congruent mod ##N##:
a) if divided by the modulus ##N##, the numbers give the same reminder. Ex: 10/5 gives reminder 0 and 0/5 gives reminder 0 as well.
b) if the difference between the two numbers ##10## and ##0## is equal to an integer multiple of the modulus ##N##. Ex: 10-0=2(5).

All that said, I believe that in computer programming, the use of the modulo operator ##%## really gives the congruent number that is in the range of numbers of the modulus. For example, 5%2 means that we are working with modulus 2 so the only possible numbers are 0 and 1, and the number 5 is congruent to number 1... So ##5%2## really gives as a result a number that is congruent to the divisor and not really the reminder.

I guess I am confused on the relation between the modulus and the modulo and if and how they are the same thing...

Thanks!
 
Mathematics news on Phys.org
Yes, the modulo operator is related to the modulus in math.

If you recall the 12-hour clock is a good example of modulo arithmetic. If it is now 11 o'clock and we work for 2 hours, then it will be 1 o'clock (not 13 o'clock).

Python:
for ihour in range(1, 24):
    print("the time is now: %d o'clock or %04d hours military time."%(ihour%12, ihour*100)

and the printout is shown below where you will note:

1) python uses the % character in multiple ways depending on context:
- as a modulo operator when in an arithmetic expression
- as a format operator when binding with a format string
- as a format specifier ("%d and %04d) inside a format specification.

2) Just as in math the % evaluates ihour%12 to 0 when ihour=12 and we would need to add something to our program to "adjust" that in our program to get a better listing of o'clock hours to military hours. (this will be left as an exercise for the student)

$ python hourtime.py

the time is now: 1 o'clock or 0100 hours military time.
the time is now: 2 o'clock or 0200 hours military time.
the time is now: 3 o'clock or 0300 hours military time.
the time is now: 4 o'clock or 0400 hours military time.
the time is now: 5 o'clock or 0500 hours military time.
the time is now: 6 o'clock or 0600 hours military time.
the time is now: 7 o'clock or 0700 hours military time.
the time is now: 8 o'clock or 0800 hours military time.
the time is now: 9 o'clock or 0900 hours military time.
the time is now: 10 o'clock or 1000 hours military time.
the time is now: 11 o'clock or 1100 hours military time.

the time is now: 0 o'clock or 1200 hours military time.

the time is now: 1 o'clock or 1300 hours military time.
the time is now: 2 o'clock or 1400 hours military time.
the time is now: 3 o'clock or 1500 hours military time.
the time is now: 4 o'clock or 1600 hours military time.
the time is now: 5 o'clock or 1700 hours military time.
the time is now: 6 o'clock or 1800 hours military time.
the time is now: 7 o'clock or 1900 hours military time.
the time is now: 8 o'clock or 2000 hours military time.
the time is now: 9 o'clock or 2100 hours military time.
the time is now: 10 o'clock or 2200 hours military time.
the time is now: 11 o'clock or 2300 hours military time.
 
fog37 said:
I guess I am confused on the relation between the modulus and the modulo and if and how they are the same thing...
No they are not the same thing: ## 10 \equiv 0 \pmod 5 ## is an equivalence reation and this concept does not exist in (most) computer languages. However they are related in the way you describe.

Note that different languages specify modular arithmetic in different ways: in particular how they work (or throw errors) if either of the operands are not integers and how they work with negative numbers.
Python:
print(4.75 % 2.25) # 0.25
print(4.75 % -2.25) # -2
print(-4.75 % 2.25) # 2
print(-4.75 % -2.25) # -0.25
JavaScript:
  console.log(4.75 % 2.25) // 0.25
  console.log(4.75 % -2.25) // 0.25
  console.log(-4.75 % 2.25) // -0.25
  console.log(-4.75 % -2.25) // -0.25
 
  • Like
Likes fog37 and jedishrfu
Thank you everyone. In summary:

In computer programming, the remainder (modulus) operator ##%## gives the integer remainder of the integer division. Ex: ##5%2 = 1## because the divisor 2 fits exactly twice into the dividend 5 leaving a 1 behind as integer leftover.

The other perspective is that the ##%## operator calculates the number that the divisor 5 is congruent to mod 2 since ##5 == 1 mod 2##. So the result of ##%##, the 1, is both a reminder and the congruent of the dividend if the dividend is outside the range of the mod 2.

The modulus operator ##%## gives the integer remainder of the integer division which is always equivalent to the number that the divisor is congruent to modulo M.

Thanks!
 
Er, no.

fog37 said:
In computer programming, the remainder (modulus) operator ##%## gives the integer remainder of the integer division. Ex: ##5%2 = 1## because the divisor 2 fits exactly twice into the dividend 5 leaving a 1 behind as integer leftover.
As I said above there is no universal "in computer programming" answer; different languages specify a modulus operator in different ways (and sometimes different for different types or even different between different language versions).

The remainder of your assertions are only true in general where both operands are positive integers.
 
For programming language comparisons, here's a link to the rosettacode.org website a tabla rasa of examples in different languages:

https://rosettacode.org/wiki/Modular_arithmetic

Please be aware that the examples may be a bit fanciful ie add more frills than necessary but they are interesting nonetheless.
 
Fermat's Last Theorem has long been one of the most famous mathematical problems, and is now one of the most famous theorems. It simply states that the equation $$ a^n+b^n=c^n $$ has no solutions with positive integers if ##n>2.## It was named after Pierre de Fermat (1607-1665). The problem itself stems from the book Arithmetica by Diophantus of Alexandria. It gained popularity because Fermat noted in his copy "Cubum autem in duos cubos, aut quadratoquadratum in duos quadratoquadratos, et...
Suppose ,instead of the usual x,y coordinate system with an I basis vector along the x -axis and a corresponding j basis vector along the y-axis we instead have a different pair of basis vectors ,call them e and f along their respective axes. I have seen that this is an important subject in maths My question is what physical applications does such a model apply to? I am asking here because I have devoted quite a lot of time in the past to understanding convectors and the dual...
Thread 'Imaginary Pythagorus'
I posted this in the Lame Math thread, but it's got me thinking. Is there any validity to this? Or is it really just a mathematical trick? Naively, I see that i2 + plus 12 does equal zero2. But does this have a meaning? I know one can treat the imaginary number line as just another axis like the reals, but does that mean this does represent a triangle in the complex plane with a hypotenuse of length zero? Ibix offered a rendering of the diagram using what I assume is matrix* notation...
Back
Top