Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!mcvax!boring!lambert From: lambert@boring.uucp (Lambert Meertens) Newsgroups: net.lang Subject: Re: Integer division Message-ID: <6762@boring.UUCP> Date: Thu, 6-Feb-86 07:25:03 EST Article-I.D.: boring.6762 Posted: Thu Feb 6 07:25:03 1986 Date-Received: Sun, 9-Feb-86 06:34:48 EST References: <332@ism780c.UUCP> <11603@ucbvax.BERKELEY.EDU> Reply-To: lambert@boring.UUCP (Lambert Meertens) Distribution: net Organization: CWI, Amsterdam Lines: 44 Apparently-To: rnews@mcvax In article <484@hoptoad.uucp> laura@hoptoad.UUCP (Laura Creighton) writes: > In article <11689@ucbvax.BERKELEY.EDU> weemba@brahms.UUCP (Matthew P. Wiener) > writes: >> .... There are several identities >> running around that are incompatible. >> >> (1) a == (a/b) * b + a%b >> (2) (-a)/b == -(a/b) >> (3) (a+b)/b == a/b + 1 >> (4) (a+b)%b == a%b >> >> Notice that (3) and (4) are compatible with what the number theorists want, >> but (2) isn't. Sure the naive user is fooled by (2) under the version we >> want, but then he's fooled by (3) and (4) in the usual version. [...] > [...] To non-mathematicians, 2 is a law, with about the same force as > the law of gravity, and not something that you can redefine. [...] > [They] will look at (-a)/b != -(a/b) and say ***BUG!!!*** I should think that to most people remembering something about what they were taught at school (3) is a forceful law as well. Together, (2) and (3) imply (1/2)*2 = 1 (which I dimly remember as also having been taught:-). ALGOL 68 has (2) and (4), and so lacks (1) and (3). A note I wrote about this (A note on integral division, ALGOL Bull. 39 (1976), 30-32) came to the conclusion that dropping (2) in favour of (1) and (3) would have been preferable. Next to arguments about what naive users would expect (where you obviously can't have it all), an important question is what is the most *useful* (convenient) set of laws in programming. Sometimes (2) is more useful, but my experience from over 25 years of programming is that (3) is the more convenient law in the majority of cases, even for non-mathematical problems. For a language for personal computing that I co-designed, honouring naive expectations was one of the design principles. The problem has been resolved there by the drastic measure of not providing a primitive operation for integral division. The mod function obeys (4). -- Lambert Meertens ...!{seismo,okstate,garfield,decvax,philabs}!lambert@mcvax.UUCP CWI (Centre for Mathematics and Computer Science), Amsterdam