From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA13655; Fri, 9 Nov 2001 14:53:40 +0100 (MET) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA13764 for caml-list@pauillac.inria.fr; Fri, 9 Nov 2001 14:53:40 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA09224 for ; Fri, 9 Nov 2001 11:31:09 +0100 (MET) Received: from fw-cam.cambridge.arm.com (fw-cam.cambridge.arm.com [193.131.176.3]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id fA9AV4n03925 for ; Fri, 9 Nov 2001 11:31:09 +0100 (MET) Received: by fw-cam.cambridge.arm.com; id KAA26818; Fri, 9 Nov 2001 10:31:02 GMT Received: from unknown(172.16.1.2) by fw-cam.cambridge.arm.com via smap (V5.5) id xma026123; Fri, 9 Nov 01 10:30:09 GMT Received: from pc31.cambridge.arm.com (localhost [127.0.0.1]) by cam-admin0.cambridge.arm.com (8.9.3/8.9.3) with ESMTP id KAA28509; Fri, 9 Nov 2001 10:30:09 GMT Received: (from egrimley@localhost) by pc31.cambridge.arm.com (8.9.3/8.9.3) id KAA24779; Fri, 9 Nov 2001 10:30:08 GMT X-Authentication-Warning: pc31.cambridge.arm.com: egrimley set sender to edmundo@rano.org using -f Date: Fri, 9 Nov 2001 10:30:08 +0000 From: Edmund GRIMLEY EVANS To: caml-list@inria.fr Subject: Re: [Caml-list] Integer arithmetic: mod Message-ID: <20011109103008.GA22605@rano.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.23.1i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk I'm responding to some old messages I found in the archive, and I'm not subscribed, so please forgive the intrusion, but recently I've been looking at how different languages define integer division and remainder becaue I had to implement some compiler optimisations involving those functions. I strongly advise against leaving the meaning of any built-in or library function or operator as implementation-defined. If you do this you will get unportable programs and inefficient programs (because people who want their programs to be portable will be forced to define their own versions of the functions). In my opinion and in most people's opinion, as far as I can tell, if you're starting afresh, the best way to define integer division is as rounding downwards. Integer remainder, to be consistent with this, has the sign of the divisor. There are lots of arguments that support this type of division, both mathematical and practical, and the only arguments against it seem to involve compatibility: the other sort of division is faster on some widely used hardware, is required by some widely used programming languages and assumed by some existing software. A good way to keep everyone happy and make sure people don't get caught out by their own assumptions is to provide both sorts. If you do this, the best names to use are DIV and MOD for the good functions (rounding downwards) and QUOT and REM for the legacy functions (rounding towards 0). These are exactly the names used in Haskell and ML and consistent with the names used in Ada, Prolog, Ruby and Scheme. (I admit I'm not very sure about Prolog. If anyone has a copy of the ISO standard, please check this for me.) Here's a little table I made (I hope it doesn't contain too many errors): BAD LEGACY! GOOD! LANGUAGE Division rounding towards 0 Division rounding downwards Remainder has sign of dividend Remainder has sign of divisor ----------------------------------------------------------------------- Ada / rem mod C89 div[quot/rem] C9x / % div[quot/rem] Fortran 90 / MOD MODULO Haskell quot rem div mod Java / % JVM idiv irem ML quot rem div mod Modula-2 div mod Perl % Prolog // rem mod Python / // % Ruby remainder / modulo divmod Scheme quotient remainder modulo Edmund Keywords: negative integer division round truncate quotient remainder number theory ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr