Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site mmintl.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!linus!philabs!pwa-b!mmintl!franka From: franka@mmintl.UUCP (Frank Adams) Newsgroups: net.lang.c,net.arch Subject: Re: Integer division Message-ID: <1133@mmintl.UUCP> Date: Mon, 3-Feb-86 19:05:07 EST Article-I.D.: mmintl.1133 Posted: Mon Feb 3 19:05:07 1986 Date-Received: Sun, 9-Feb-86 05:14:59 EST References: <332@ism780c.UUCP> <11603@ucbvax.BERKELEY.EDU> Reply-To: franka@mmintl.UUCP (Frank Adams) Distribution: net Organization: Multimate International, E. Hartford, CT Lines: 37 Xref: watmath net.lang.c:7794 net.arch:2480 We seem to agree that there are three at least somewhat important identities. 1) (a/b)*b + a%b = a 2) (a+b)%b = a%b or (a+b)/b = a/b + 1 3) (-a)%b = -(a%b) or (-a)/b = -(a/b) As a mathematician and as a computer scientist, I cannot accept definitions of these functions for which (1) does not hold. Given (1), the two forms given for (2) and (3) are equivalent. Now, in fact, (3) in the division form is important. The area I know of where it is important is in financial applications. Suppose I own 200 shares of stock, which I purchased at a total cost of $2,098.75, including commission. I now sell 100 shares. I have to compute the cost basis for those 100 shares: $1,049.38. Now, suppose I had a short position with the same cost basis: -$2,098.75. If I buy back half of these, the rounding has to be done the same way: -$1,049.38. Of course, this application is not rounding toward zero; it is rounding to the *nearest* penny. So what we want for this application is to round to the nearest integer, with 1/2 rounded away from zero. This choice is very common in financial applications. (By the way, financial applications fairly often divide by negative numbers.) There are also a lot of number theoretic algorithms which run faster if the least absolute remainder is used; I once heard a professor of mathematics (Hans Zassenhaus, if memory serves) state that the least absolute remainder is what computer division *should* return. I believe that computers (CISC) and programming languages should provide at least three different division and remainder operations: round towards 0, round towards -infinity, and round to nearest (with 1/2 rounded away from 0). There is something to be said for round away from zero, and round to +infinity, as well. Frank Adams ihpn4!philabs!pwa-b!mmintl!franka Multimate International 52 Oakland Ave North E. Hartford, CT 06108