caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] truncated division, remainder and arithmetics
@ 2016-01-27  0:34 peio
  2016-01-27  9:56 ` François Bobot
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: peio @ 2016-01-27  0:34 UTC (permalink / raw)
  To: caml-list

Good evening,


while doing some modular arithmetic I discovered that OCaml uses the
'truncated division' convention for the `/` (quotient) operator.
Actually this convention may seem innocent but it greatly affects the
`mod` (remainder) operator: the sign of the result is the same as
dividend.

After some research I realized that lots of people (D.Knuth!)
criticized this convention in favor of floored division (sign of
remainder same as divisor) or euclidean division (remainder always
positive). I know such a key component of the language isn't likely to
be changed but I would like to get some of the rationals behind this
decision.


I can't think of any case in which the current behavior more natural
but there are clearly situation giving weird results: I encountered one
while multiplying big numbers modulo 2**62 (on 64-bit machine): as
`max_int` is 2**62 - 1 the product could overflow and become negative.
Consequently one can't just write `(a * b) mod m` to get the result of
multiplication on the group Z/mZ if a and b are bigger than 2**31.

The workaround I found was to redefine quotient and remainder to follow
floored division convention:
let rem a b = ((a mod b) + b) mod b
let quo
a b = (a - rem a b) / b
Does anyone see a cleaner way to do it?

I don't know anything about division algorithm, but is it actually
easier/faster to implement the truncated division instead of the
floored division? Is there any other reason I missed to choose
truncated division?


At last another thing that (slightly) bugs me: why don't `ceil` and
`floor` have the type `float -> int`? Because the inherent definitions
of these functions talks about integers, not floats (floor(x) is the
biggest integer less than x). Indeed the `int_of_float (ceil x)` is a
very common pattern (according to some very unscientific searching
through github code search). Maybe even more subtle is that `truncate`
has type `float -> int`. In my opinion the type `float -> float` would
be more appropriate here because the truncation operation is about
approximating a real number with a decimal (we already got
`int_of_float` for rough converting).


cheers,
peio

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

end of thread, other threads:[~2016-01-28 19:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-27  0:34 [Caml-list] truncated division, remainder and arithmetics peio
2016-01-27  9:56 ` François Bobot
2016-01-27 10:02 ` Hendrik Boom
2016-01-27 10:18 ` Xavier Leroy
2016-01-28 19:03   ` peio

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