caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "François Bobot" <francois.bobot@cea.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] truncated division, remainder and arithmetics
Date: Wed, 27 Jan 2016 10:56:59 +0100	[thread overview]
Message-ID: <56A8946B.4000109@cea.fr> (raw)
In-Reply-To: <1453854887.31205.2.camel@gmail.com>

On 27/01/2016 01:34, peio wrote:
> 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 think it is just because of the assembly instruction available
http://x86.renejeschke.de/html/file_module_x86_id_137.html .

If you are doing computation with big numbers perhaps you can take a look at zarith, which define 
many of the different division convention:

```
external div: t -> t -> t = "ml_z_div" "ml_as_z_div"
(** Integer division. The result is truncated towards zero
     and obeys the rule of signs.
     Raises [Division_by_zero] if the divisor (second argument) is 0.
  *)

external rem: t -> t -> t = "ml_z_rem" "ml_as_z_rem"
(** Integer remainder. Can raise a [Division_by_zero].
     The result of [rem a b] has the sign of [a], and its absolute value is
     strictly smaller than the absolute value of [b].
     The result satisfies the equality [a = b * div a b + rem a b].
  *)

external div_rem: t -> t -> (t * t) = "ml_z_div_rem"
(** Computes both the integer quotient and the remainder.
     [div_rem a b] is equal to [(div a b, rem a b)].
     Raises [Division_by_zero] if [b = 0].
  *)

external cdiv: t -> t -> t = "ml_z_cdiv"
(** Integer division with rounding towards +oo (ceiling).
     Can raise a [Division_by_zero].
  *)

external fdiv: t -> t -> t = "ml_z_fdiv"
(** Integer division with rounding towards -oo (floor).
     Can raise a [Division_by_zero].
  *)

val ediv_rem: t -> t -> (t * t)
(** Euclidean division and remainder.  [ediv_rem a b] returns a pair [(q, r)]
     such that [a = b * q + r] and [0 <= r < |b|].
     Raises [Division_by_zero] if [b = 0].
  *)

val ediv: t -> t -> t
(** Euclidean division. [ediv a b] is equal to [fst (ediv_rem a b)].
     The result satisfies [0 <= a - b * ediv a b < |b|].
     Raises [Division_by_zero] if [b = 0].
  *)

val erem: t -> t -> t
(** Euclidean remainder.  [erem a b] is equal to [snd (ediv_rem a b)].
     The result satisfies [0 <= erem a b < |b|] and
     [a = b * ediv a b + erem a b].  Raises [Division_by_zero] if [b = 0].
  *)
```

Best,

-- 
François

  reply	other threads:[~2016-01-27  9:57 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 [this message]
2016-01-27 10:02 ` Hendrik Boom
2016-01-27 10:18 ` Xavier Leroy
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=56A8946B.4000109@cea.fr \
    --to=francois.bobot@cea.fr \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).