remainder of dividing by zero
Terry Reedy
tjreedy at udel.edu
Thu Apr 12 19:37:08 EDT 2012
On 4/12/2012 6:34 PM, Ethan Furman wrote:
> Okay, so I haven't asked a stupid question in a long time and I'm
> suffering withdrawal symptoms... ;)
>
> 5 % 0 = ?
>
> It seems to me that the answer should be 5: no matter how many times we
> add 0 to itself, the remainder of the intermediate step will be 5.
>
> Is there a postulate or by definition answer as to why this should not
> be so?
0 <= M % N < N
no solution for N == 0
m // n, m % n = divmod(m, n), which is to say, divmod is the fundamental
recursively defined operation and // and % are each one of the
components with the other ignored. m % n = divmod(m, n)[1]
and divmod is not defined for n == 0
def divmod(m, n): # m, n not negative
q, r = 0, m
while m >= n:
q, r = q+1, r-n
return q, r
Of course, given m, n in base n representation, the way you learned to
do it in school is faster. In binary, it is even easier because each
digit is 0 or 1 so no guessing needed as with base 10.
--
Terry Jan Reedy
More information about the Python-list
mailing list