How Does Modulus Divison Work

后端 未结 19 1553
栀梦
栀梦 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: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.

提交回复
热议问题