caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Yaron Minsky <yminsky@janestreet.com>
To: Stephen Dolan <stephen.dolan@cl.cam.ac.uk>
Cc: Hannes Mehnert <hannes@mehnert.org>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] behaviour of mod
Date: Tue, 20 Jan 2015 16:57:33 -0500	[thread overview]
Message-ID: <CACLX4jR0Ujx5pwhYLweRxKU8NVs8Aa5iChfDwCz04fvXd8kt3A@mail.gmail.com> (raw)
In-Reply-To: <CA+mHimPfz8R6gKeGBzLP2RATEFLv8jOj-Bdd0Cvrfo08YgS2Zw@mail.gmail.com>

Core has an answer to this too.  Core's Int_intf has two extra
operations, %, for the traditional modular arithmetic mod operator,
and /%, for the corresponding division operator.  Here's the comment
on it.  Note that Int.rem is just the mod operator (but we also have
Int32.rem, Int64.rem, etc.)

  (** There are two pairs of integer division and remainder functions,
      [/%] and [%], and [/] and [rem].  They both satisfy the same
      equation relating the quotient and the remainder:

      {[
        x = (x /% y) * y + (x % y);
        x = (x /  y) * y + (rem x y);
      ]}

      The functions return the same values if [x] and [y] are
      positive.  They all raise if [y = 0].

      The functions differ if [x < 0] or [y < 0].

      If [y < 0], then [%] and [/%] raise, whereas [/] and [rem] do
      not.

      [x % y] always returns a value between 0 and [y - 1], even when
      [x < 0].  On the other hand, [rem x y] returns a negative value
      if and only if [x < 0]; that value satisfies [abs (rem x y) <=
      abs y - 1]. *)

On Mon, Jan 19, 2015 at 5:59 AM, Stephen Dolan
<stephen.dolan@cl.cam.ac.uk> wrote:
> On Sun, Jan 18, 2015 at 6:48 PM, Hannes Mehnert <hannes@mehnert.org> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA384
>>
>> Hi,
>>
>> is the behaviour of modulo arithmetics intentional:
>>  -5 mod 4 = -1 ?
>>
>> While this reflects the C behaviour, my expectation was to always have
>> a positive result:
>>  -5 mod 4 = 3
>>
>> Any hints?
>
> Given OCaml's integer division operator, this is the correct "mod".
>
> The important property is:
>
>     (x / y) * y + (x mod y) = x
>
> In other words, (x mod y) gives the error by which (x / y) * y fails to equal x.
>
> There are two reasonable (/, mod) pairs which have this behaviour. The
> first, which C and OCaml use, is where (x / y) rounds towards zero and
> (x mod y) has the same sign as x, so -5 / 4 = -1 and -5 mod 4 = -1.
> The second is where (x / y) rounds down and (x mod y) has the same
> sign as y, so -5 / 4 = -2 and -5 mod 4 = 3.
>
> Subjectively, I think the first division operator (round-toward-zero,
> aka truncate) and the second modulo operator are the more natural. The
> second modulo operator actually implements modular arithmetic, since
> with it x mod n buckets the integers x into n equivalence classes
> differing by multiples of n. But using the first (/) and the second
> mod breaks the remainder property above.
>
> Incidentally, Haskell provides both: the first is called (quot, rem)
> while the second is (div, mod).
>
> Stephen
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2015-01-20 21:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-18 18:48 Hannes Mehnert
2015-01-18 19:28 ` Gabriel Scherer
2015-01-18 21:06 ` Mr. Herr
2015-01-19 10:59 ` Stephen Dolan
2015-01-20 21:57   ` Yaron Minsky [this message]
2015-01-20 21:57     ` Yaron Minsky

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=CACLX4jR0Ujx5pwhYLweRxKU8NVs8Aa5iChfDwCz04fvXd8kt3A@mail.gmail.com \
    --to=yminsky@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=hannes@mehnert.org \
    --cc=stephen.dolan@cl.cam.ac.uk \
    /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).