caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* RE: [Caml-list] Integer arithmetic: mod
@ 2001-08-06 13:23 Dave Berry
  0 siblings, 0 replies; 15+ messages in thread
From: Dave Berry @ 2001-08-06 13:23 UTC (permalink / raw)
  To: caml-list

The Standard ML Basis Library also has both div/mod and rem/quot.  (This
was after some people complained that the original language definition
required the slower, Knuth-approved, behaviour).



-----Original Message-----
From: Marcin 'Qrczak' Kowalczyk [mailto:qrczak@knm.org.pl]
Sent: 04 August 2001 21:26
To: caml-list@inria.fr
Subject: Re: [Caml-list] Integer arithmetic: mod


Sat, 04 Aug 2001 11:48:07 -0700, Chris Hecker <checker@d6.com> pisze:

> Most computer languages (and chips) simply say "(a/b)*b + a mod b =
> a" and leave it at that.

Fortunately not all, e.g. in Python (-123) % 10 == 7. In C89 the
behavior for negative numbers was left implementation-defined but
in C99 it is specified as truncation towards 0.

> Unfortunately, people (and language and chip designers) assume
> (-4)/3 = -1 (truncate towards zero) rather than -2 (floor),

Not all people: Donald Knuth clearly disagrees. He has written
something along the lines "beware of programming languages which use
a different definition than the one which says (-4)/3 = -2".

Haskell has both: div & mod truncate downwards, quot & rem truncate
towards 0.

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK

-------------------
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
-------------------
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


^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: [Caml-list] Integer arithmetic: mod
@ 2001-11-19 16:39 Krishnaswami, Neel
  0 siblings, 0 replies; 15+ messages in thread
From: Krishnaswami, Neel @ 2001-11-19 16:39 UTC (permalink / raw)
  To: 'caml-list@inria.fr'

Xavier Leroy [mailto:xavier.leroy@inria.fr] wrote:
> 
> I'm favorable to providing proper Euclidean division and modulus as
> library functions. The way I learned Euclidean division in college 
> is that the quotient q and the modulus r of a divided by b are 
> defined by
>
>       a = b * q + r  with 0 <= r < |b|
>
> Any mathematician on this list who could look it up in Bourbaki?

I haven't checked Bourbaki, but this is my understanding as well.

There's also a paper on the subject in the ACM TOPLAS from 1992. 
"The Euclidean definition of the functions div and mod", by Raymond 
Boute:

http://portal.acm.org/citation.cfm?id=128862&coll=portal&dl=ACM&CFID=745503&
CFTOKEN=75650609#FullText

--
Neel Krishnaswami
neelk@cswcasa.com
 
-------------------
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


^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: [Caml-list] Integer arithmetic: mod
@ 2001-11-09 10:30 Edmund GRIMLEY EVANS
  2001-11-19 15:49 ` Xavier Leroy
  0 siblings, 1 reply; 15+ messages in thread
From: Edmund GRIMLEY EVANS @ 2001-11-09 10:30 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 15+ messages in thread
[parent not found: <9khicj$3n3$1@qrnik.zagroda>]
* [Caml-list] Integer arithmetic: mod
@ 2001-08-04 10:49 Kai Kaminski
  2001-08-04 18:48 ` Chris Hecker
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Kai Kaminski @ 2001-08-04 10:49 UTC (permalink / raw)
  To: caml-list

Hi,

I have a question regarding the 'mod' operator in OCaml. While playing
around with the toplevel, I found that

-1 mod 10 = -1 instead of -1 mod 10 = 9.

Looking into the Handbook showed me, that the behaviour of 'mod' is
platform dependent for negative arguments.
Now, I could live with -1 mod 10 = -1, but why is it platform-dependent?
As far as I can see it would be better to specify a certain behaviour
and emulate it on platforms, where it is not directly supported by the
cpu, especially for a high-level language as OCaml.
The reason is that whenever such a behaviour is platform-dependent, you
can't use it at all, as long as you want to have your programs portable.
Beside that the speed penalty (are there other problems with this
approach?) would probably be not too bad, would it?

On the other hand I'm just a student, who doesn't know too much about
programming languages and their implementation. So could someone please
enlighten me?

Kai Kaminski
-------------------
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


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2001-11-19 16:45 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-06 13:23 [Caml-list] Integer arithmetic: mod Dave Berry
  -- strict thread matches above, loose matches on Subject: below --
2001-11-19 16:39 Krishnaswami, Neel
2001-11-09 10:30 Edmund GRIMLEY EVANS
2001-11-19 15:49 ` Xavier Leroy
2001-11-19 16:48   ` Vesa Karvonen
     [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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).