Modulo and Modulus in math and computer programming

In summary: In Python, the modulus is always an integer number from 0 to 5, and the modulus operator is just a convenient notation for saying "convert this number from base 10 to base 5".
  • #1
fog37
1,569
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
  • #2
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.
 
  • Like
Likes fog37
  • #3
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
  • #4
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!
 
  • #5
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.
 
  • #6
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.
 
  • Like
Likes fog37

FAQ: Modulo and Modulus in math and computer programming

What is the difference between "modulo" and "modulus"?

Modulo and modulus are often used interchangeably, but they have slightly different meanings. Modulo refers to the mathematical operation of finding the remainder when one number is divided by another. Modulus, on the other hand, refers to the numerical value of the remainder itself. For example, in the expression 17 % 5, the modulo is 2 and the modulus is 5.

How is modulo used in computer programming?

Modulo is commonly used in computer programming to perform a variety of tasks, such as determining if a number is even or odd, generating random numbers, and implementing cyclic patterns. It is also useful for checking divisibility and controlling loops and iterations.

What is the syntax for using modulo in programming languages?

The syntax for using modulo varies slightly between programming languages, but it typically involves using the % symbol between two numbers. For example, in Python, the expression 17 % 5 would return the value 2. In Java, the same expression would be written as 17 % 5 = 2.

Can modulo be used with non-integer numbers?

Yes, modulo can be used with non-integer numbers, but the result may not be intuitive. For example, in most programming languages, the expression 5.5 % 2 would return the value 1.5, as 5.5 divided by 2 has a remainder of 1.5. However, some programming languages may round the result to the nearest integer, so it is important to check the specific language's syntax and rules.

Are there any common mistakes when using modulo in programming?

One common mistake when using modulo in programming is forgetting to include the % symbol, which can result in the entire expression being evaluated as a division instead of a modulo operation. Another mistake is using the wrong order of operands, as the order matters when using modulo. For example, 5 % 2 would return 1, but 2 % 5 would return 2. It is important to double-check the syntax and order of operands when using modulo in programming.

Similar threads

Back
Top