From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 71B777EFCD for ; Wed, 15 Oct 2014 22:25:53 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of edwin+ml-ocaml@etorok.net) identity=pra; client-ip=62.113.205.31; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="edwin+ml-ocaml@etorok.net"; x-sender="edwin+ml-ocaml@etorok.net"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of edwin+ml-ocaml@etorok.net designates 62.113.205.31 as permitted sender) identity=mailfrom; client-ip=62.113.205.31; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="edwin+ml-ocaml@etorok.net"; x-sender="edwin+ml-ocaml@etorok.net"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of postmaster@mx.etorok.net designates 62.113.205.31 as permitted sender) identity=helo; client-ip=62.113.205.31; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="edwin+ml-ocaml@etorok.net"; x-sender="postmaster@mx.etorok.net"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgsFAD/XPlQ+cc0f/2dsb2JhbABbgw6BK9NUAoEXFgF9hAIBAQEDAUA4AgQLCxgJEwMPCQMCAQIBRRMIAogyDLJdhgCQQwaQGzoWhDUBi1+ReZYagUcMgic6L4JKAQEB X-IPAS-Result: AgsFAD/XPlQ+cc0f/2dsb2JhbABbgw6BK9NUAoEXFgF9hAIBAQEDAUA4AgQLCxgJEwMPCQMCAQIBRRMIAogyDLJdhgCQQwaQGzoWhDUBi1+ReZYagUcMgic6L4JKAQEB X-IronPort-AV: E=Sophos;i="5.04,727,1406584800"; d="scan'208";a="83395521" Received: from mx.etorok.net ([62.113.205.31]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 15 Oct 2014 22:25:52 +0200 Received: by mx.etorok.net (OpenSMTPD) with ESMTP id 6e47e52e; for ; Wed, 15 Oct 2014 23:25:50 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=etorok.net; h= message-id:date:from:mime-version:to:references:in-reply-to :content-type:content-transfer-encoding; s=ml; l=1889; bh=khFFEd Ngn1z+zYEWN7m/J6uTTA0=; b=IOpr1X1OB1BN//Bom5dIbdf0i3kPTQ/i3ogLWR Wab6c+PB2D9o37WvMDXoqQUGW2eDeSrdX1inInZi1YKm9XaOXcsjbpdbrc5kExNg pd9t9qJq+TYrNF4tDbSw1z+O+73xno0aLokJgHfXj08Ecn6fpH68i3g7KhWv4Xn9 vReX4= Received: by mx.etorok.net (OpenSMTPD) with ESMTPSA id a8aa6fd7; TLS version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO; for ; Wed, 15 Oct 2014 23:25:50 +0300 (EEST) Message-ID: <543ED84D.4010302@etorok.net> Date: Wed, 15 Oct 2014 23:25:49 +0300 From: =?windows-1252?Q?T=F6r=F6k_Edwin?= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.1.2 MIME-Version: 1.0 To: caml-list@inria.fr References: <20141015112211.3e6bb721@xivilization.net> In-Reply-To: <20141015112211.3e6bb721@xivilization.net> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] Divide and print with precision On 10/15/2014 12:22 PM, Marek Kubica wrote: > Hello, > > I've got this short program here: > > let prec = 1_000_000 Do you mean this to be bits or digits? AFAICT the ~prec in Gmp refers to bits. > let max_n = 205_211 > let to_string = Gmp.F.to_string_base_digits ~base:10 ~digits:0 > > let euler_fraction n = > let open Z in > let numerator = ref one in > let denominator = ref one in > for i = 1 to n do > numerator := succ (!numerator * (of_int i)); > denominator := (of_int i) * !denominator; > done; > (!numerator, !denominator) > > let f () = > let (num, den) = euler_fraction max_n in > let znum = Gmp.F.from_string (Z.to_string num) in > let zden = Gmp.F.from_string (Z.to_string den) in This uses Gmp.default_prec (120 bits by default) for the conversion. So if you want to use Gmp.F I think you have to specify the ~prec otherwise you might loose digits in the znum or zden conversion already: let znum = Gmp.F.from_string_prec_base ~prec ~base:10 (Z.to_string num) in let zden = Gmp.F.from_string_prec_base ~prec ~base:10 (Z.to_string den) in let f = Gmp.F.div_prec ~prec znum zden in Gmp.F.to_string_base_digits ~base:10 ~digits:0 f Another possibility is to use from_q_prec. I would've used Gmp.Q.from_q_prec except for some odd reason it takes a Z.t instead of a Q.t, so here is the code that uses Gmp.FR.from_q_prec: let string_of_q_prec num den = let znum = Gmp.Z.from_string (Z.to_string num) in let zden = Gmp.Z.from_string (Z.to_string den) in let f = Gmp.FR.from_q_prec ~prec ~mode (Gmp.Q.from_zs znum zden) in Gmp.FR.to_string_base_digits ~mode ~base:10 ~digits:0 f I don't really like going through string to convert from Z.t to Gmp.Z.t, there ought to be a more efficient way. There is also Num.approx_num_fix, but if you already use Zarith/Gmp you probably don't want that. Best regards, --Edwin