Modulus division when first number is smaller than second number

前端 未结 6 736
时光取名叫无心
时光取名叫无心 2021-01-04 00:20

I apologize if this is a simple question but I\'m having trouble grasping the concept of modulus division when the first number is smaller than the second number. For exampl

6条回答
  •  生来不讨喜
    2021-01-04 00:56

    I am going to add a more practical example to what "Jean-Bernard Pellerin" already said.

    It is correct that if you divide 1 by 4 you get 0 but, Why when you do 1 % 4 you have 1 as result?

    Basically it is because this:

    n = a / b (integer), and
    m = a % b = a - ( b * n )
    

    So,

     a    b    n = a/b  b * n  m = a%b
     1    4      0        0      1    
     2    4      0        0      2
     3    4      0        0      3
     4    4      1        0      0
     5    4      1        4      1
    

    Conclusion: While a < b, the result of a % b will be "a"

提交回复
热议问题