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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 4BB4C7F0EF for ; Wed, 27 Jan 2016 10:57:00 +0100 (CET) IronPort-PHdr: 9a23:HjcqDxGNLghVIEUgCm/+L51GYnF86YWxBRYc798ds5kLTJ75o8ywAkXT6L1XgUPTWs2DsrQf27WQ6/qrATxIyK3CmU5BWaQEbwUCh8QSkl5oK+++Imq/EsTXaTcnFt9JTl5v8iLzG0FUHMHjew+a+SXqvnYsExnyfTB4Ov7yUtaLyZ/niKbqodaLM01hv3mUX/BbFF2OtwLft80b08NJC50a7V/3mEZOYPlc3mhyJFiezF7W78a0+4N/oWwL46pyv50IbaKvdK09SflcDS86G2Ez/szi8xfZHiWV4X5JfH8flJVPAhPyxhbwWYu55iXgv+x23yiBe8fxR7cocT+v5uFlUkm72288Kzcl/TSP2YRLh6VBrUf5qg== Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=francois.bobot@cea.fr; spf=None smtp.mailfrom=francois.bobot@cea.fr; spf=None smtp.helo=postmaster@sainfoin-out.extra.cea.fr Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of francois.bobot@cea.fr) identity=pra; client-ip=132.167.192.145; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="francois.bobot@cea.fr"; x-sender="francois.bobot@cea.fr"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of francois.bobot@cea.fr) identity=mailfrom; client-ip=132.167.192.145; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="francois.bobot@cea.fr"; x-sender="francois.bobot@cea.fr"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@sainfoin-out.extra.cea.fr) identity=helo; client-ip=132.167.192.145; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="francois.bobot@cea.fr"; x-sender="postmaster@sainfoin-out.extra.cea.fr"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BRAQAGlKhWnJHAp4RehAxtiFe0DiKFbQKBRjwQAQEBAQEBAQEQAQEBAQEGDQkJIS6CLYIVAQEEIwQLAQVAEQsYAgIFFgsCAgkDAgECAUUTCAKIFw6uGo82DB57iiOFF4I1gToFlnuFR5E3hVOOQzeCP4FZaQGIQwEBAQ X-IPAS-Result: A0BRAQAGlKhWnJHAp4RehAxtiFe0DiKFbQKBRjwQAQEBAQEBAQEQAQEBAQEGDQkJIS6CLYIVAQEEIwQLAQVAEQsYAgIFFgsCAgkDAgECAUUTCAKIFw6uGo82DB57iiOFF4I1gToFlnuFR5E3hVOOQzeCP4FZaQGIQwEBAQ X-IronPort-AV: E=Sophos;i="5.22,354,1449529200"; d="scan'208";a="161856447" Received: from sainfoin-out.extra.cea.fr ([132.167.192.145]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 27 Jan 2016 10:56:59 +0100 Received: from pisaure.intra.cea.fr (pisaure.intra.cea.fr [132.166.88.21]) by sainfoin.extra.cea.fr (8.15.2/8.15.2/CEAnet-Internet-out-2.4) with ESMTP id u0R9uxpT004272 for ; Wed, 27 Jan 2016 10:56:59 +0100 Received: from pisaure.intra.cea.fr (localhost [127.0.0.1]) by localhost (Postfix) with SMTP id 553D0203A2E for ; Wed, 27 Jan 2016 11:04:56 +0100 (CET) Received: from muguet2.intra.cea.fr (muguet2.intra.cea.fr [132.166.192.7]) by pisaure.intra.cea.fr (Postfix) with ESMTP id 4D09E2039D1 for ; Wed, 27 Jan 2016 11:04:56 +0100 (CET) Received: from [10.8.32.80] (is222783.intra.cea.fr [10.8.32.80]) by muguet2.intra.cea.fr (8.15.2/8.15.2/CEAnet-Intranet-out-1.4) with ESMTP id u0R9uxsZ001681 for ; Wed, 27 Jan 2016 10:56:59 +0100 To: caml-list@inria.fr References: <1453854887.31205.2.camel@gmail.com> From: =?UTF-8?Q?Fran=c3=a7ois_Bobot?= Message-ID: <56A8946B.4000109@cea.fr> Date: Wed, 27 Jan 2016 10:56:59 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:43.0) Gecko/20100101 Icedove/43.0 MIME-Version: 1.0 In-Reply-To: <1453854887.31205.2.camel@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Caml-list] truncated division, remainder and arithmetics 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