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.
 
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Thread 'Unit Circle Double Angle Derivations'
Here I made a terrible mistake of assuming this to be an equilateral triangle and set 2sinx=1 => x=pi/6. Although this did derive the double angle formulas it also led into a terrible mess trying to find all the combinations of sides. I must have been tired and just assumed 6x=180 and 2sinx=1. By that time, I was so mindset that I nearly scolded a person for even saying 90-x. I wonder if this is a case of biased observation that seeks to dis credit me like Jesus of Nazareth since in reality...
Back
Top