How does modulus of a smaller dividend and larger divisor work?

后端 未结 10 1259
误落风尘
误落风尘 2020-12-15 04:47
7 % 3 = 1 (remainder 1)

how does
3 % 7 (remainder ?)

work?

相关标签:
10条回答
  • 2020-12-15 05:00

    As long as they're both positive, the remainder will be equal to the dividend. If one or both is negative, then you get reminded that % is really the remainder operator, not the modulus operator. A modulus will always be positive, but a remainder can be negative.

    0 讨论(0)
  • 2020-12-15 05:01

    The most simple and effective catch to remember would be: Whenever dividend is less than the divisor, modulus is just that dividend.

    Let's formulate this:

    if x < y, then x % y = x 
    
    0 讨论(0)
  • 2020-12-15 05:02

    7 goes into 3? zero times with 3 left over.

    quotient is zero. Remainder (modulus) is 3.

    0 讨论(0)
  • 2020-12-15 05:02

    a % q = r means there is a x so that q * x + r = a.

    So, 7 % 3 = 1 because 3 * 2 + 1 = 7,

    and 3 % 7 = 3 because 7 * 0 + 3 = 3

    0 讨论(0)
  • 2020-12-15 05:04

    remainder of 3/7 is 3..since it went 0 times with 3 remainder so 3%7 = 3

    0 讨论(0)
  • 2020-12-15 05:05

    The same way. The quotient is 0 (3 / 7 with fractional part discarded). The remainder then satisfies:

    (a / b) * b + (a % b) = a
    (3 / 7) * 7 + (3 % 7) = 3
    0 * 7 + (3 % 7) = 3
    (3 % 7) = 3
    

    This is defined in C99 §6.5.5, Multiplicative operators.

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