Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: N Hur <mapnh@maths.bath.ac.uk>, caml-list@inria.fr
Subject: Re: Is this OK?
Date: Mon, 18 May 1998 14:51:54 +0200	[thread overview]
Message-ID: <19980518145154.41389@pauillac.inria.fr> (raw)
In-Reply-To: <Pine.SUN.3.91.980515135653.27393A-100000@thor.maths.bath.ac.uk>; from N Hur on Fri, May 15, 1998 at 02:02:24PM +0100

> Why are the two answers below different?
> (* a camllight session *)
> #(-234)/4
> ;;
> - : int = -58
> 
> #div_big_int (big_int_of_string "-234") (big_int_of_string "4");;
> - : big_int = -59
> Thanks for advance for any explanation.

In computer arithmetic, integer division has never been well specified
for non-positive arguments.  There are at least two sensible behaviors:

        (a) round towards zero          e.g. (-1) / 2 = 0
        (b) round towards -infinity     e.g. (-1) / 2 = -1

Each of these two behaviors satisfies two of the following three
desirable properties of integer division and modulus, but not all
three:

        (1)   (-a) / b = a / (-b) = - (a/b)
        (2)   a = (a/b) * b + (a mod b)
        (3)   0 <= a mod b < abs(b)

More precisely, (a) satisfies (1) and (2) but not (3) (the modulus can
be negative), while (b) satisfies (2) and (3) but fails (1).  

It is easy to see that (1), (2) and (3) cannot be satisfied
simultaneously anyway, so behaviors (a) and (b) are as good as you'll
ever get.

Most processors implement behavior (a), and therefore most C programs
also follow (a)  (though the C reference manual does not fully specify
integer division and modulus for negative arguments, and therefore (a)
and (b) are both legal).  Since the Caml bytecode interpreter is a
C program, that's also the behavior you get.

The bignum library uses its own signed arithmetic over big integers,
and elected to implement behavior (b), i.e. round towards -infinity,
thus ensuring that the modulus is always positive.  The reason for
this choice, I guess, is that (b) is much better than (a) if you're
doing modulo arithmetic.

So, there is basically no solution to this problem, except providing
two division operations and two modulus operations, one set following
(a) and the other following (b).

- Xavier Leroy





  parent reply	other threads:[~1998-05-18 13:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-05-15 13:02 N Hur
1998-05-18  8:03 ` Jean-Christophe Filliatre
1998-05-18 12:51 ` Xavier Leroy [this message]
1998-05-18 13:59 Damien Doligez

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=19980518145154.41389@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=mapnh@maths.bath.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