From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTP id ABCE47FBC5 for ; Tue, 20 Jan 2015 22:57:52 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of yminsky@janestreet.com) identity=pra; client-ip=38.105.200.112; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="yminsky@janestreet.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of yminsky@janestreet.com designates 38.105.200.112 as permitted sender) identity=mailfrom; client-ip=38.105.200.112; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="yminsky@janestreet.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mxout1.mail.janestreet.com) identity=helo; client-ip=38.105.200.112; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="postmaster@mxout1.mail.janestreet.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtQAAMXOvlQmachwnGdsb2JhbABbDoNKWASDA7Nkj1KFcQKBIgdDAQEBAQERAQEBAQEGFglChA0BAQQSER0BASwLAQ8LCw0CAgkdAgIiEgEFAQoSBhMIChCHdgMRAwqwJj4xij5whGIBBYsOA4RaAQEBAQEBAQMBAQEBAQEBAQETBgqBF45YB4JogUGEQwaFMYgrhVCBSoRlijESI4EVgiMdgRNbUQGCQgEBAQ X-IPAS-Result: AtQAAMXOvlQmachwnGdsb2JhbABbDoNKWASDA7Nkj1KFcQKBIgdDAQEBAQERAQEBAQEGFglChA0BAQQSER0BASwLAQ8LCw0CAgkdAgIiEgEFAQoSBhMIChCHdgMRAwqwJj4xij5whGIBBYsOA4RaAQEBAQEBAQMBAQEBAQEBAQETBgqBF45YB4JogUGEQwaFMYgrhVCBSoRlijESI4EVgiMdgRNbUQGCQgEBAQ X-IronPort-AV: E=Sophos;i="5.09,437,1418079600"; d="scan'208";a="117889132" Received: from mxout1.mail.janestreet.com ([38.105.200.112]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 20 Jan 2015 22:57:51 +0100 Received: from [172.27.56.106] (helo=tot-qpr-mailcore2) by mxout1.mail.janestreet.com with smtp (Exim 4.82) (envelope-from ) id 1YDgo2-00050A-KJ for caml-list@inria.fr; Tue, 20 Jan 2015 16:57:50 -0500 X-JS-Flow: external Received: by tot-qpr-mailcore2 with JS-mailcore (0.1) (envelope-from ) id BUvs9e-AAACfA-TQ; 2015-01-20 16:57:50.617205-05:00 Received: from mail-la0-f42.google.com ([209.85.215.42]) by mxgoog1.mail.janestreet.com with esmtps (TLSv1:RC4-SHA:128) (Exim 4.72) (envelope-from ) id 1YDgo2-0007sV-8o for caml-list@inria.fr; Tue, 20 Jan 2015 16:57:50 -0500 Received: by mail-la0-f42.google.com with SMTP id ms9so14722121lab.1 for ; Tue, 20 Jan 2015 13:57:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=yZTZ2ogAnU2SsHs9G/OQZAjktkgoWQhgRC0/VRScDgA=; b=1Pg5VaA4GUIL3jzPVBCMJy70PRQVucYT7lRapVlmHhqes2oFnUXzL7pjiaD6ZLLU7A T3VNwr87e3a2DW75hYbYSDloc2sDhqyznYaJRLJRMFIXjV6DUfgZFCBm19pd2id1DwP/ 3JVPQENNUTqGu350wxGLifwkyWqbgvPcR5h3w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=yZTZ2ogAnU2SsHs9G/OQZAjktkgoWQhgRC0/VRScDgA=; b=EDtmyRwCVq8CgDBer6xeJRaRr9lz9wJCLZsBE/fL6RGey8OfRlN9gCUm4QDQuRRBDd b7yk/ISAY+k6Hcn+56ARZ4CmBagK1FK/bgvlAxg5cBCldghuImwthc6wG1Y3V+8BqlaS YLnHzEdCPn7qNdsfIBOxM0PSBfnUExuDGsaUPAJf2ddRfBAmZM5vs2CnaYN6nosbz2i5 UpT99iuWx14YN/lebEKRHI4y4NcDr5syPPHaWMxJsD9Q7hCazLiLQp2PBt+wW5kgqbCk JbLEpf4aUtn+mYsUIb/4KEMu4WRsqsvaD2Kyy2JX2xRK811AXHDZyTOAWGzw48fN7DCl L2TA== X-Gm-Message-State: ALoCoQnBxWrJSgBdKcHv2Ig8xSZykeWmJ/7luAHPdZNQf2UJGHpbfPNxg9RDaa07DtBjf5DX57seeX6vNonVxGfGQ78SlYK3XoLM7oGwV+BzCmN8IQfizpMdnCiTul/Ly18BCuTf1RTk X-Received: by 10.152.206.41 with SMTP id ll9mr41221429lac.62.1421791069215; Tue, 20 Jan 2015 13:57:49 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.152.206.41 with SMTP id ll9mr41221423lac.62.1421791069105; Tue, 20 Jan 2015 13:57:49 -0800 (PST) Received: by 10.113.5.40 with HTTP; Tue, 20 Jan 2015 13:57:49 -0800 (PST) In-Reply-To: References: <54BBFFF7.6030106@mehnert.org> Date: Tue, 20 Jan 2015 16:57:49 -0500 Message-ID: From:Yaron Minsky To:Stephen Dolan Cc:Hannes Mehnert , caml-list Content-Type: text/plain; charset=UTF-8 X-JS-Processed-by: mailcore Subject: Re: [Caml-list] behaviour of mod (And this is all in Core_kernel, the portable subset of Core, actually.) y On Tue, Jan 20, 2015 at 4:57 PM, Yaron Minsky wrote: > 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 > wrote: >> On Sun, Jan 18, 2015 at 6:48 PM, Hannes Mehnert 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