Programing

Modulus Divison은 어떻게 작동합니까?

crosscheck 2020. 8. 26. 07:23
반응형

Modulus Divison은 어떻게 작동합니까?


나는 모듈러스 분할이 어떻게 작동하는지 정말로 이해하지 못합니다. 나는 계산 27 % 16하고 끝냈고 11왜 그런지 이해하지 못합니다.

온라인에서 평신도 용어로 설명을 찾을 수없는 것 같습니다. 누군가 여기서 무슨 일이 일어나고 있는지 매우 높은 수준으로 설명 할 수 있습니까?


모듈로 나눗셈 의 결과 는 주어진 숫자 정수 나눗셈나머지입니다 .

그것의 의미는:

27 / 16 = 1, remainder 11
=> 27 mod 16 = 11

다른 예 :

30 / 3 = 10, remainder 0
=> 30 mod 3 = 0

35 / 3 = 11, remainder 2
=> 35 mod 3 = 2

대부분의 설명은 한 가지 중요한 단계를 놓치고 있습니다. 다른 예를 사용하여 공백을 메우겠습니다.

다음을 고려하면 :

Dividend: 16
Divisor: 6

계수 기능은 다음과 같습니다 :

16 % 6 = 4

이것이 왜 그런지 결정합시다.

먼저 정수 나누기를 수행 합니다 . 이는 분수 (일명 나머지)가 삭제된다는 점을 제외하면 일반 나눗셈과 유사합니다.

16 / 6 = 2

그런 다음 위의 나눗셈 ( ) 의 결과에 제수 ( )를 합니다 .26

2 * 6 = 12

마지막으로 배당금 ( ) 에서 위의 곱셈 ( ) 의 결과를 뺍니다 .1216

16 - 12 = 4

이 뺄셈의 결과는, 4나머지는 , 우리 같은 결과 계수 위!


시계가있는 예제는 모듈로를 이해하는 데 도움이 될 수 있습니다.

모듈 식 산술의 익숙한 사용은 하루를 두 개의 12 시간 주기로 나누는 12 시간 시계에서 사용하는 것입니다.

현재 시간이 15:00 이라고 가정 해 보겠습니다.
하지만 오후 3 시라고 도 할 수 있습니다.

이것이 바로 모듈로가하는 일입니다 :

15 / 12 = 1, remainder 3

이 예제는 wikipedia에서 더 잘 설명되어 있습니다. Wikipedia Modulo Article


계수를 계산하는 간단한 공식은 다음과 같습니다.

[Dividend-{(Dividend/Divisor)*Divisor}]

그래서, 27 % 16 :-

27- {(27/16) * 16}

27- {1 * 16}

답 = 11

참고 :

모든 계산은 정수로 이루어집니다. 소수 몫의 경우 소수 뒤의 부분은 무시 / 잘립니다.

예 : 27 / 16 = 1.6875는 위에서 언급 한 공식에서 단지 1로 간주됩니다. 0.6875는 무시됩니다.

컴퓨터 언어의 컴파일러는 소수 부분이있는 정수도 동일한 방식으로 처리합니다 (소수점 뒤에서 잘림).


The modulus operator takes a division statement and returns whatever is left over from that calculation, the "remaining" data, so to speak, such as 13 / 5 = 2. Which means, there is 3 left over, or remaining from that calculation. Why? because 2 * 5 = 10. Thus, 13 - 10 = 3.

The modulus operator does all that calculation for you, 13 % 5 = 3.


modulus division is simply this : divide two numbers and return the remainder only

27 / 16 = 1 with 11 left over, therefore 27 % 16 = 11

ditto 43 / 16 = 2 with 11 left over so 43 % 16 = 11 too


Very simple: a % b is defined as the remainder of the division of a by b.

See the wikipedia article for more examples.


I hope these simple steps will help:

20 % 3 = 2 
  1. 20 / 3 = 6; do not include the .6667 – just ignore it
  2. 3 * 6 = 18
  3. 20 - 18 = 2, which is the remainder of the modulo

I would like to add one more thing:

it's easy to calculate modulo when dividend is greater/larger than divisor

dividend = 5 divisor = 3

5 % 3 = 2

3)5(1
  3
-----
  2

but what if divisor is smaller than dividend

dividend = 3 divisor = 5

3 % 5 = 3 ?? how

This is because, since 5 cannot divide 3 directly, modulo will be what dividend is


Modulus division gives you the remainder of a division, rather than the quotient.


Lets say you have 17 mod 6.

what total of 6 will get you the closest to 17, it will be 12 because if you go over 12 you will have 18 which is more that the question of 17 mod 6. You will then take 12 and minus from 17 which will give you your answer, in this case 5.

17 mod 6=5


Modulus division is pretty simple. It uses the remainder instead of the quotient.

    1.0833... <-- Quotient
   __
12|13
   12
    1 <-- Remainder
    1.00 <-- Remainder can be used to find decimal values
     .96
     .040
     .036
     .0040 <-- remainder of 4 starts repeating here, so the quotient is 1.083333...

13/12 = 1R1, ergo 13%12 = 1.


It helps to think of modulus as a "cycle".

In other words, for the expression n % 12, the result will always be < 12.

That means the sequence for the set 0..100 for n % 12 is:

{0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11,0,[...],4}

In that light, the modulus, as well as its uses, becomes much clearer.


Easier when your number after the decimal (0.xxx) is short. Then all you need to do is multiply that number with the number after the division.

Ex: 32 % 12 = 8

You do 32/12=2.666666667 Then you throw the 2 away, and focus on the 0.666666667 0.666666667*12=8 <-- That's your answer.

(again, only easy when the number after the decimal is short)


The only important thing to understand is that modulus (denoted here by % like in C) is defined through the Euclidean division.

For any two (d, q) integers the following is always true:

d = ( d / q ) * q + ( d % q )

As you can see the value of d%q depends on the value of d/q. Generally for positive integers d/q is truncated toward zero, for instance 5/2 gives 2, hence:

5 = (5/2)*2 + (5%2) => 5 = 2*2 + (5%2) => 5%2 = 1

However for negative integers the situation is less clear and depends on the language and/or the standard. For instance -5/2 can return -2 (truncated toward zero as before) but can also returns -3 (with another language).

In the first case:

-5 = (-5/2)*2 + (-5%2) => -5 = -2*2 + (-5%2) => -5%2 = -1

but in the second one:

-5 = (-5/2)*2 + (-5%2) => -5 = -3*2 + (-5%2) => -5%2 = +1

As said before, just remember the invariant, which is the Euclidean division.

Further details:


It's simple, Modulus operator(%) returns remainder after integer division. Let's take the example of your question. How 27 % 16 = 11? When you simply divide 27 by 16 i.e (27/16) then you get remainder as 11, and that is why your answer is 11.


Write out a table starting with 0.

{0,1,2,3,4}

Continue the table in rows.

{0,1,2,3,4}
{5,6,7,8,9}
{10,11,12,13,14}

Everything in column one is a multiple of 5. Everything in column 2 is a multiple of 5 with 1 as a remainder. Now the abstract part: You can write that (1) as 1/5 or as a decimal expansion. The modulus operator returns only the column, or in another way of thinking, it returns the remainder on long division. You are dealing in modulo(5). Different modulus, different table. Think of a Hash Table.


When we divide two integers we will have an equation that looks like the following:

A/B​​ =Q remainder R

A is the dividend; B is the divisor; Q is the quotient and R is the remainder

Sometimes, we are only interested in what the remainder is when we divide A by B. For these cases there is an operator called the modulo operator (abbreviated as mod).

Examples

16/5= 3 Remainder 1  i.e  16 Mod 5 is 1.
0/5= 0 Remainder 0 i.e 0 Mod 5 is 0.
-14/5= 3 Remainder 1 i.e. -14 Mod 5 is 1.

See Khan Academy Article for more information.

In Computer science, Hash table uses Mod operator to store the element where A will be the values after hashing, B will be the table size and R is the number of slots or key where element is inserted.

See How does a hash table works for more information


This was the best approach for me for understanding modulus operator. I will just explain to you through examples.

16 % 3

When you division these two number, remainder is the result. This is the way how i do it.

16 % 3 = 3 + 3 = 6; 6 + 3 = 9; 9 + 3 = 12; 12 + 3 = 15

So what is left to 16 is 1

16 % 3 = 1

Here is one more example: 16 % 7 = 7 + 7 = 14 what is left to 16? Is 2 16 % 7 = 2

One more: 24 % 6 = 6 + 6 = 12; 12 + 6 = 18; 18 + 6 = 24. So remainder is zero, 24 % 6 = 0

참고URL : https://stackoverflow.com/questions/2664301/how-does-modulus-divison-work

반응형