Why Does 0 % 5 Return 0?
Solution 1:
Division is defined so that the following is always true
n = q × d + r
where
- n is the numerator (or dividend),
- d != 0 is the denominator (or divisor),
- q is the quotient, and
- r > 0 is the remainder.
(This holds for positive and negative values; q is positive if n and d have the same sign and negative otherwise. r is defined to be always positive.)
In Python, n/d == q
and n % d == r
. If n
is 0, then q
must also be 0, in which case r
must be 0 as well—all independent of the value of d
.
(Off-topic, but note that this also captures the problem with division by 0: for non-zero d, q and r are uniquely determined; for d = 0, any value of q will satisfy the equation for r = n.
Solution 2:
Why does 0 % 5 return 0?
Because:
Zero divided by five is zero, remains zero.
0 % 5 = 0
12 % 5 = 2
Solution 3:
a % n is the same as a - (n * int(a/n)). 0/5 equals 0 because 5 goes in to 0, 0 times. 0 * 5 is 0. 0 minus 0 is 0.
Solution 4:
Try if self.num_hits / 5 == 0:
Solution 5:
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
7 % 5 = 2
...
Post a Comment for "Why Does 0 % 5 Return 0?"