How does a modulo operation work when the first number is smaller?
Asked Answered
S

19

80

I'm messing with the modulo operation in python and I understand that it will spit back what the remainder is.

But what if the first number is smaller than the second?

for instance

2 % 5 the answer is 2.

How does that work?

2/5 = .4

Staphylorrhaphy answered 8/10, 2009 at 4:39 Comment(5)
You've asserted that "2/5 = .4", but that's wrong. Try typing "2/5" into the REPL.Kraken
12 % 5 = 2 because 5 x 2 = 10 and 12 - 10 = 2Quiescent
2 % 5 = 2 because 5 x 0 = 0 and 2 - 0 = 2Quiescent
@Quiescent Great explanation--you should turn it into an answer.Gabelle
@ChrisMartin only in 2.x. (At the time of your comment, Python 3.3 was actively developed.)Whelm
C
61

Does this help

22  % 5 = 2 
17  % 5 = 2 
12  % 5 = 2 
7   % 5 = 2 
2   % 5 = 2

Maybe this

22 / 5 = 4 + 2/5
17 / 5 = 3 + 2/5
12 / 5 = 2 + 2/5
7  / 5 = 1 + 2/5
2  / 5 = 0 + 2/5
Chinkapin answered 8/10, 2009 at 4:49 Comment(0)
L
51

5 goes into 2 zero times.

5*0 = 0

2-0 = 2.

The answer is 2.

Lir answered 8/10, 2009 at 4:40 Comment(0)
K
29

2 divided by 5 (integer division) is 0 with a remainder of 2.

Koestler answered 8/10, 2009 at 4:45 Comment(0)
B
20

It's really supper easy to figure out the results of modulo when the first number is smaller. The result is always equal the the first (smaller) number

3 % 5 = 3
5 % 10 = 5
78 % 112 = 78

Try it out for yourself.

Bureau answered 14/6, 2013 at 22:6 Comment(0)
C
15

2 = 0 x 5 + 2

Carcinoma answered 8/10, 2009 at 4:55 Comment(0)
I
14

If the first number is smaller, then the answer is that first number again.

Because the second number is larger, it 'goes into' the first number zero times and the remainder is the entirety of this first number.

edit: revisiting this thread, I had to remember what this operator was for. I referred to this other thread here:

Recognizing when to use the modulus operator

Ischium answered 15/11, 2018 at 15:34 Comment(0)
C
10

for instance 2 % 5 the answer is 2. How does that work? 2/5 = .4!

Modulo inherently produces an integer result, whereas division can be an integer or floating point operation. Your observation that 2/5 equals 0.4 indicates you're thinking in terms of floating point. In that case, the .4 itself is the remainder, expressed differently. The integral portion of "0.4" is the "0" and the remainder portion is ".4". The remainder of an integer division operation is exactly the same thing as the fractional (or "decimal", in colloquial terms) portion of a floating point operation, just expressed differently.

The fractional part of your example, 0.4, can be expressed as 0.4 or as 2/5 (two fifths); either way it's the same thing. Note that when it's written as 2/5, the denominator (divisor) of the fractional part is the same as the denominator (divisor) of the original problem, while the numerator (dividend) of the fractional part is what is referred to as the "remainder" in integer division. Any way you look at it, the fractional part of the quotient and the remainder represent the same thing (the portion of the dividend that cannot be evenly divided by the divisor), just expressed differently.

Crush answered 2/3, 2012 at 16:43 Comment(3)
No, the .4 is the quotient, and there is no remainder, in real division. The rest of this doesn't make sense either, as it implies that the correct modulus value is 0.4 rounded or truncated to zero.Waly
@EJP - I'm sorry my answer didn't make sense to you. In "real" division, the dividend divided by the divisor results in the quotient. The quotient can be expressed in different ways: It can be expressed as single quantity (in various forms) or as an integer representing the integral number of times the dividend can be evenly divided by the divisor, and if there is any amount "left over" that can't be evenly divided by the divisor, that's the "remainder". The remainder is exactly that portion of the quotient that would fall to the right of the "decimal point", only expressed differently.Crush
User in the first comment is wrong to contest this just because they didn't understand. This makes perfect sense and is a great explanation of the math behind the operator. Thinking about a 'remainder' as a different way to express a fractional component of the result of division was helpful to me!Pipage
O
6

You can think of it as 2 / 5 = 0 with a remainder of 2 of 5.

Overmatter answered 8/10, 2009 at 4:40 Comment(0)
P
2

a % b = a if a << b

Pyrophosphate answered 8/10, 2009 at 4:42 Comment(2)
I think you mean if 0 < a < b ... if a is negative then you're in trouble.Woodcock
nice catch I should have been more precise.Pyrophosphate
T
2

The numerator in the remainder is your modulo answer, no matter what, whether the numerator is bigger or smaller than the denominator.

12 % 5 = 2 , because 12 / 5 = 2 and **2**/5

9 % 2 = 1 , because 9 / 2 = 4 and **1**/2

This may make more sense.

5 % 89 = 5 , because 5 / 89 = 0 and **5**/89

5 % 365 = 5 , because 5 / 365 = 0 and **5**/365

5 % 6 = 5 , because 5 / 6 = 0 and **5**/6
Tactic answered 2/2, 2015 at 3:25 Comment(0)
S
2
a%b = a/b=c,
      c*b=d,
      a-d=modulo;

This is it what python does while mod two numbers or so i think. The modulo between a smaller number and a bigger number will always be the smaller number.

Sylas answered 16/2, 2016 at 10:11 Comment(2)
Is this a new answer? What is the new information comparing to the others?Iz
Comment without read the commentaries. Then read them but couldn't erase my commentary. I apologize i suppose.Sylas
O
2

Another thing to note was that if the first number (a) is a negative number, the answer is always the difference of the second number to the first number (n-a).

Example: a % n

  1. -5 % 7 = 2 ---> 7 - 5 = 2
  2. 4 % -9 = -5 ---> 9 - 4 = -5 (follow the sign of the larger number)

If both numbers were negative, the answer will always be a negative number which is equal to the smaller number.

  1. -5 % -7 = -5
  2. -4 % -9 = -4
Overarm answered 24/7, 2017 at 4:47 Comment(0)
E
2

Just as a follow up for us non-math-brain people out there, I think part of the problem in understanding this is that the concept is often simplified as "what's left over when you divide x by y", which, when x is smaller, is nothing, aka 0. I more fail proof way might be to say

  1. How many times does y fully go into x?
  2. Take that number, and multiply it by y to get as close to x as possible
  3. Now subtract what you get from step 2 from x, that's your modulo.

So in 2 (as x) % 5 (as y):

  1. 5 goes into 2 fully no times at all, so, 0
  2. 0 (the outcome of the step above) multiplied by 5(aka y) is 0
  3. 2(aka x) - 0 (the product from the step above) is 2
Excommunicatory answered 20/3, 2021 at 22:0 Comment(0)
B
1

MOD doesnt work with decimal... MOD(A,B) u want result where A

Brickey answered 11/1, 2017 at 5:2 Comment(1)
Could you either extend on this a little or add it as a comment instead? It might be hard for some people to follow your thinking here. Thank you!Walden
S
1

There's no number that would multiply 5 to get you closer to 2.

In other words x has to be 0 to satisfy the equation: 5*x + r = 2.

Selfopinionated answered 22/1, 2017 at 1:9 Comment(0)
L
1

To understand modular arithmetic, I suggest you go over to Khan Academy and read their post about it. They also have interactive practice questions on the same page. Here's the link: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic

In short:

Use the following equation:

A = BQ + R

A is the dividend

B is the divisor

Q is the quotient

R is the remainder, and is the result for a modulo.

Q = (A/B)

Keep in mind that Q always goes to the closest smallest integer. So if Q = 0.2, then Q = 0.0. If Q = -1.2, then Q = -2.0.

If Q doesn't have decimals then your final answer is 0 (R = 0).


For your question:

Q = (2/5) = 0.4, so Q = 0.

Plug that into 'A = BQ + R':

2 = 5*0 + R

So, R = 2.


Hope this helps. As I said you can read more about on Khan Academy. Here's the link: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic

Lothair answered 10/7, 2018 at 4:20 Comment(0)
K
1

In case i.e. 2%5 = 2: In real math remainder is 0 and quotient is 0.4 but in programming, it see 2 is less than 5 means 5*1=5, so it go low and just divide it by 0 so it become less which is 0. Hence remainder become 2 because 2-0=2 ;)

Kibler answered 20/11, 2020 at 21:18 Comment(0)
A
0

Example: a % n

  1. -5 % 7 = 2 ---> 7 - 5 = 2
  2. 4 % -9 = -5 ---> 9 - 4 = -5 (follow the sign of the larger number)

[^ Actually for this part depend on the programming language itself, no.1 it can be either 2 or -5 bypass the negative sign, or no.2 can be either -5 or 4]

@aldennis

Acey answered 18/7, 2023 at 9:54 Comment(0)
M
-1

Modulo works by giving the remainder after division, one thing it's useful for is
-Finding if a number is even or not
Code sample:

// 4 % 2 means "2 divided by 2 is what, and what is the remainder? if I have a remainder, return it or else return 0"
if(4 % 2 == 0) {
  alert("2 is even");
} else {
  alert("2 is odd");
}

So if 4 % 2 has a remainder of 0, it's even or else it's odd.

Martino answered 4/6, 2021 at 18:37 Comment(2)
This is a "JavaScript" version of Modulo.Martino
How does this address the actual question of what happens when the first operand is smaller than the second?Faiyum

© 2022 - 2024 — McMap. All rights reserved.