This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Re: [Caml-list] Integer arithmetic: mod
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: Xavier Leroy 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

```