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

六月ゝ 毕业季﹏ 提交于 2019-12-18 10:26:09

问题


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


回答1:


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



回答2:


five goes into 2 zero times.

5*0 = 0

2-0 = 2.

the answer is 2




回答3:


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




回答4:


2 = 0 x 5 + 2




回答5:


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.




回答6:


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.




回答7:


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




回答8:


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




回答9:


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



回答10:


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



回答11:


a % b = a if a << b




回答12:


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.




回答13:


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


回答14:


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.




回答15:


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.


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



来源:https://stackoverflow.com/questions/1535656/how-does-a-modulo-operation-work-when-the-first-number-is-smaller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!