Browse thread
Re: [Caml-list] Integer arithmetic: mod
-
Edmund GRIMLEY EVANS
-
Xavier Leroy
- Vesa Karvonen
-
Xavier Leroy
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Vesa Karvonen <vesa.karvonen@h...> |
| Subject: | Re: [Caml-list] Integer arithmetic: mod |
IIRC, the appendix on arithmetic in Computer Architecture: A Quantitative Approach by Hennessy and Patterson (ISBN: 1558603298) has some rather convincing arguments on the superior definition of integer div and mod. Well worth reading. Regards, Vesa Karvonen ----- Original Message ----- From: "Xavier Leroy" <xavier.leroy@inria.fr> To: "Edmund GRIMLEY EVANS" <edmundo@rano.org> Cc: <caml-list@inria.fr> Sent: Monday, November 19, 2001 17:49 Subject: Re: [Caml-list] Integer arithmetic: mod > > 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). > > I can agree with this argument. > > > 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. > > Well, all hardware today implements round-towards-zero for division, > and this is unlikely to change in the future since ISO C9x requires > this behavior, so this will remain the behavior of "/" in OCaml. > We certainly do not want to penalize the existing programs that use > "/" and "mod" correctly, i.e. on positive arguments. > > I'm favorable to providing proper Euclidean division and modulus as > library functions. But: I disagree with your statement that > > > the best way to define integer division is as > > rounding downwards. Integer remainder, to be consistent with this, has > > the sign of the divisor. > > 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| > > e.g. the modulus is never negative, and division does not necessarily > rounds downwards. I believe what mathematically-oriented minds really > want is a modulus that is always positive. > > Any mathematician on this list who could look it up in Bourbaki? > > - Xavier Leroy > ------------------- > 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