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 ESMTPS id 7A3BF7F6D8 for ; Tue, 20 Jan 2015 22:57:38 +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: AtQAAMXOvlQmachwnGdsb2JhbABbDoNKWASDA7Nkj1KFcQKBIgdDAQEBAQERAQEBAQEGFglChA0BAQQSER0BASwLAQ8LCw0CAgkdAgIiEgEFAQoSBhMIChCHdgMRAwqwJj4xij5whGIBBYsOA4RaAQEBAQEBBAEBAQEBAQEBARMGCoEXjlgHgmiBQYRDBoUxiCuFUIFKhGWKMRIjgRWCIx2BE1tRAYJCAQEB X-IPAS-Result: AtQAAMXOvlQmachwnGdsb2JhbABbDoNKWASDA7Nkj1KFcQKBIgdDAQEBAQERAQEBAQEGFglChA0BAQQSER0BASwLAQ8LCw0CAgkdAgIiEgEFAQoSBhMIChCHdgMRAwqwJj4xij5whGIBBYsOA4RaAQEBAQEBBAEBAQEBAQEBARMGCoEXjlgHgmiBQYRDBoUxiCuFUIFKhGWKMRIjgRWCIx2BE1tRAYJCAQEB X-IronPort-AV: E=Sophos;i="5.09,437,1418079600"; d="scan'208";a="117889045" 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:37 +0100 Received: from [172.27.56.106] (helo=tot-qpr-mailcore2) by mxout1.mail.janestreet.com with smtp (Exim 4.82) (envelope-from ) id 1YDgnn-0004vt-BP for caml-list@inria.fr; Tue, 20 Jan 2015 16:57:35 -0500 X-JS-Flow: external Received: by tot-qpr-mailcore2 with JS-mailcore (0.1) (envelope-from ) id BUvs9P-AAACfA-Km; 2015-01-20 16:57:35.340139-05:00 Received: from mail-lb0-f170.google.com ([209.85.217.170]) by mxgoog1.mail.janestreet.com with esmtps (TLSv1:RC4-SHA:128) (Exim 4.72) (envelope-from ) id 1YDgnm-0007sC-Ve for caml-list@inria.fr; Tue, 20 Jan 2015 16:57:35 -0500 Received: by mail-lb0-f170.google.com with SMTP id 10so35925073lbg.1 for ; Tue, 20 Jan 2015 13:57:33 -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=EstZfthSwr7nxMmPF0Efk2IK67cxeVrYNnX4rJlk6oI=; b=VETtn14jrhWNBZywbT0CDi03EL/nY+iEs8OVvh/uEp7bCgWke9IlaVpBTFOiMgn9ys fIXXjBXd5UrCcGUOIoENgrTbBBHkaERwgLiFw2Ydh/YdFr1Iuz0ChSoRa0O9x3ngwvTq bJG2lRDYby2bPh6XR0zBAJVnSD+3bcv5HLGOI= 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=EstZfthSwr7nxMmPF0Efk2IK67cxeVrYNnX4rJlk6oI=; b=YapJZSGgAp8pB4x4RdziWgkjUwxec8Hujc/FdVCwJ2Hr7Nd3dS4kxi3ZPEf8NSiHqM y8Itk6zradxwSFnwsm7/nzX5GFrYz9ZjKtC8zd2mvEDb795jDC1Sj3TaqK3U55i0b+3k PXf+efkDq2XxN//HoXUkgOIWKSveq9cTbTwKjGqd2yod1oc7g22zgXYqhbRJ9RBDusPq D5GjmE8Dspdn/uCxcYSw6w1FlH5c9+TbTJzdvz03ksvJsQf6F9cTcwnQ7M3wTiy507eL fAa3rvyso/CvIAyXMYTrgQ/lusZaFJ0vvk0EdjP/Ge4tLbcK4biioPd7oeBZ7lW4PaHI 3C9A== X-Gm-Message-State: ALoCoQlVQTFiWVMXgbovV84zjD0q/6KKImqVozGeQXoAiY/KBzl9S57CY7g9uzrroN4+0tSAK3/kP9P+DTU5xn+ajpVxkhzzNYTEDdTDc6V+dIdHHjefPE9VKxK92gUWc+OTVtz7HbZx X-Received: by 10.152.206.108 with SMTP id ln12mr40925109lac.3.1421791053936; Tue, 20 Jan 2015 13:57:33 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.152.206.108 with SMTP id ln12mr40925103lac.3.1421791053810; Tue, 20 Jan 2015 13:57:33 -0800 (PST) Received: by 10.113.5.40 with HTTP; Tue, 20 Jan 2015 13:57:33 -0800 (PST) In-Reply-To: References: <54BBFFF7.6030106@mehnert.org> Date: Tue, 20 Jan 2015 16:57:33 -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 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