From: Edmund GRIMLEY EVANS <edmundo@rano.org>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Integer arithmetic: mod
Date: Fri, 9 Nov 2001 10:30:08 +0000 [thread overview]
Message-ID: <20011109103008.GA22605@rano.org> (raw)
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
next reply other threads:[~2001-11-09 13:53 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-11-09 10:30 Edmund GRIMLEY EVANS [this message]
2001-11-19 15:49 ` Xavier Leroy
2001-11-19 16:48 ` Vesa Karvonen
-- strict thread matches above, loose matches on Subject: below --
2001-11-19 16:39 Krishnaswami, Neel
2001-08-06 13:23 Dave Berry
[not found] <9khicj$3n3$1@qrnik.zagroda>
2001-08-04 20:25 ` Marcin 'Qrczak' Kowalczyk
2001-08-05 8:05 ` Chris Hecker
2001-08-06 1:06 ` John Gerard Malecki
2001-08-04 10:49 Kai Kaminski
2001-08-04 18:48 ` Chris Hecker
2001-08-05 23:35 ` John Max Skaller
2001-08-10 22:10 ` Kai Kaminski
2001-08-06 9:10 ` Xavier Leroy
2001-08-10 22:29 ` Kai Kaminski
2001-08-13 15:21 ` Xavier Leroy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20011109103008.GA22605@rano.org \
--to=edmundo@rano.org \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox