How Does Modulus Divison Work

后端 未结 19 1570
栀梦
栀梦 2020-11-22 06:55

I don\'t really understand how modulus division works. I was calculating 27 % 16 and wound up with 11 and I don\'t understand why.

I can\'t

相关标签:
19条回答
  • 2020-11-22 07:06

    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:

    • What is the behavior of integer division?
    • Division and Modulus for Computer Scientists
    0 讨论(0)
  • 2020-11-22 07:08

    27 % 16 = 11

    You can interpret it this way:

    16 goes 1 time into 27 before passing it.

    16 * 2 = 32.

    So you could say that 16 goes one time in 27 with a remainder of 11.

    In fact,

    16 + 11 = 27

    An other exemple:

    20 % 3 = 2

    Well 3 goes 6 times into 20 before passing it.

    3 * 6 = 18

    To add-up to 20 we need 2 so the remainder of the modulus expression is 2.

    0 讨论(0)
  • 2020-11-22 07:09

    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.

    0 讨论(0)
  • 2020-11-22 07:09

    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.

    0 讨论(0)
  • 2020-11-22 07:10

    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
    0 讨论(0)
  • 2020-11-22 07:10

    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.

    0 讨论(0)
提交回复
热议问题