Xref: utzoo comp.theory:393 comp.misc:8335 comp.lang.misc:4272 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!mucs!chl From: chl@cs.man.ac.uk (Charles Lindsey) Newsgroups: comp.theory,comp.misc,comp.lang.misc Subject: Re: What if the Divisor is Negative? (was Re: Modulus) Message-ID: <1013@m1.cs.man.ac.uk> Date: 26 Feb 90 17:10:39 GMT References: <4356@daffy.cs.wisc.edu> Organization: Dept. Of Comp Sci, Univ. of Manchester, UK. Lines: 69 lori@rt7.cs.wisc.edu (Lori Lehman) writes: >Everyone will agree that > (a) 5 div 3 = 1, > (b) 5 mod 3 = 2, >and most people seem to agree that > (c) -5 div 3 = -2, > (d) -5 mod 3 = 1. >Note that the divisor in these examples, 3, is positive. What happens when the >divisor is _negative_? That is, how do we define the following? > (e) 5 div -3, > (f) 5 mod -3, > (g) -5 div -3, > (h) -5 mod -3. > >ONE POSSIBLE SOLUTION >--------------------- >Generally speaking, given numbers m and n, we want to find the quotient and the >remainder when m is divided by n. The quotient is "m div n" and the remainder >is "m mod n." Mathematically speaking, given integers m and n (n <> 0), we can >solve for q and r in the equation > m = qn + r, >where q and r are integers. If we force 0 <= r < n, we can get a unique >solution. I am delighted that "most" people are now agreed about the -ve dividends, but I do not like the proposed solution for -ve divisors. ANOTHER POSSIBLE SOLUTION ------------------------- I have no gut feeling for what 5 mod -3 should do, so let us fix div first. The consensus for -ve divisors implies that m div n = floor(m/n). Let us try to keep this for the new case. (e) 5 div -3 = -2 ( = -5 div 3 ) (g) -5 div -3 = 1 But we should like to preserve m = qn + r, which inevitably leads to (f) 5 mod -3 = -1 (h) -5 mod -3 = -2 Now try this for gut feeling, and we have, for all m and -ve n, n < m mod n <= 0 just as we had, for all m and +ve n, 0 <= m mod n < n The whole thing feels quite comfortable for me, and it just remains for some Group Theorist to report whether it is now a respectable algebra. To summarize: (a) 5 div 3 = 1 (e) 5 div -3 = -2 (b) 5 mod 3 = 2 (f) 5 mod -3 = -1 (c) -5 div 3 = -2 (g) -5 div -3 = 1 (d) -5 mod 3 = 1 (h) -5 mod -3 = -2