Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: peio <peio.borthelle@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] truncated division, remainder and arithmetics
Date: Wed, 27 Jan 2016 11:18:02 +0100	[thread overview]
Message-ID: <56A8995A.3030304@inria.fr> (raw)
In-Reply-To: <1453854887.31205.2.camel@gmail.com>

On 27/01/16 01:34, peio wrote:

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

All contemporary microprocessors that implement division in hardware
implement what you call truncated division.  The other forms of
division and modulus (there are at least two others) can be
implemented on top of that.  See this excellent summary:

Daan Leijen, Division and Modulus for Computer Scientists , July 2003.
http://research.microsoft.com/apps/pubs/default.aspx?id=151917

> 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

Yes, but with the type you propose they would overflow easily for
large enough FP arguments.  In FP computations involving ceil and
floor, it is often possible to do the computation entirely in
floating-point, avoiding catastrophic overflow cases.

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

"truncate" and "int_of_float" are (currently) the same function.

- Xavier Leroy

  parent reply	other threads:[~2016-01-27 10:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-27  0:34 peio
2016-01-27  9:56 ` François Bobot
2016-01-27 10:02 ` Hendrik Boom
2016-01-27 10:18 ` Xavier Leroy [this message]
2016-01-28 19:03   ` peio

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=56A8995A.3030304@inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=peio.borthelle@gmail.com \
    /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