Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site ucbvax.BERKELEY.EDU Path: utzoo!watmath!clyde!burl!ulysses!ucbvax!brahms!weemba From: weemba@brahms.BERKELEY.EDU (Matthew P. Wiener) Newsgroups: net.math,net.lang.c,net.arch Subject: Re: Integer division Message-ID: <11603@ucbvax.BERKELEY.EDU> Date: Wed, 29-Jan-86 20:28:58 EST Article-I.D.: ucbvax.11603 Posted: Wed Jan 29 20:28:58 1986 Date-Received: Sat, 1-Feb-86 02:49:29 EST References: <332@ism780c.UUCP> Sender: usenet@ucbvax.BERKELEY.EDU Reply-To: weemba@brahms.UUCP (Matthew P. Wiener) Distribution: net Organization: University of California, Berkeley Lines: 35 Xref: watmath net.math:2754 net.lang.c:7707 net.arch:2440 In article <332@ism780c.UUCP> tim@ism780c.UUCP (Tim Smith) writes: >[Which is preferred: (-a)%b == - (a%b) or (-a)%b >= 0 always?] >Are there any good mathematical grounds for choosing one alternative over >the other here? Note that I am not asking from a hardware point of view >which is better. I want to know mathematically if one is better than >the other. I have NEVER seen an instance where the first one is preferable. Not only is it not preferable, it is just incorrect. Why such a routine has been allowed to be 50% inaccurate in every existing language all these years is beyond me. But since that is what you get, I have always had to program around it, sometimes thinking of clever ways to prevent negative numbers from being fed into the remaindering. Even in non-mathematical usages, the second is preferred. For example, if you have a circular list of length N, kept in an array, i=change(i)%N is the usual way most steps through the list are done, for some integer function change(i). At one research institute I have worked at, IMOD is put in the Fortran library, to be used instead of MOD, so as to get the correct remainder. MOD was left for portability from other people's software, not for usage. [I pity the fool who says "but the first is faster".] [Whether CS people should even be *allowed* to make such mathematical decisions is another question. In C on UNIX, for example, one has log(-x) == log(x), a rather dangerous identity, not based on anything comprehensible. Thus, the implementation of general exponentiation, a**b = pow(a,b) = exp( b*log(a) ) will silently return the wrong value if a is negative. (Try taking cube roots this way!)] ucbvax!brahms!weemba Matthew P Wiener/UCB Math Dept/Berkeley CA 94720