Xref: utzoo comp.theory:386 comp.misc:8322 comp.lang.misc:4259 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!munnari.oz.au!goanna!ok From: ok@goanna.oz.au (Richard O'keefe) Newsgroups: comp.theory,comp.misc,comp.lang.misc Subject: Re: What if the Divisor is Negative? (was Re: Modulus) Message-ID: <2911@goanna.oz.au> Date: 26 Feb 90 07:55:17 GMT References: <4356@daffy.cs.wisc.edu> Followup-To: comp.lang.misc Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 20 In article <4356@daffy.cs.wisc.edu>, lori@rt7.cs.wisc.edu (Lori Lehman) writes: [asking about X div Y when Y < 0] The thing which keeps tripping people up is the assumption that there is *one* "proper" definition of div and mod. Common Lisp offers four: (floor x y) => q r ;; round towards -oo (ceiling x y) => q r ;; round towards +oo (truncate x y) => q r ;; round towards 0 (round x y) => q r ;; round to nearest (on tie to even) where in each case x = q*y + r and |r| < |y| I have had occasion to use _each_ of these functions, and I've been glad that the names said clearly which one I was using. It would be useful to have something else, say (quotient x y) => q r ;; undefined for x < 0 or y <= y which could do whatever was fastest on a particular machine; the use of this function would be a signal to the human reader that the arguments were expected to be in a range where it didn't matter which opcode was used (usually the case).