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 0DB257F02D for ; Mon, 20 Oct 2014 23:19:13 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of marek@xivilization.net) identity=pra; client-ip=178.63.18.39; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="marek@xivilization.net"; x-sender="marek@xivilization.net"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of marek@xivilization.net designates 178.63.18.39 as permitted sender) identity=mailfrom; client-ip=178.63.18.39; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="marek@xivilization.net"; x-sender="marek@xivilization.net"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@coaxial.xivilization.net) identity=helo; client-ip=178.63.18.39; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="marek@xivilization.net"; x-sender="postmaster@coaxial.xivilization.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkcHABN8RVSyPxIn/2dsb2JhbABcgw5kA0SCPki3L5lMAgyBCRYBfYQDAQEDAQwXHQEBOAQLCQIaAgUhAgIPSBmINwwBlCKbV3iFAgEFgQKOSwElBoEsjywWGIJJgVSdXoExhjOKPoQBg3lqgksBAQE X-IPAS-Result: AkcHABN8RVSyPxIn/2dsb2JhbABcgw5kA0SCPki3L5lMAgyBCRYBfYQDAQEDAQwXHQEBOAQLCQIaAgUhAgIPSBmINwwBlCKbV3iFAgEFgQKOSwElBoEsjywWGIJJgVSdXoExhjOKPoQBg3lqgksBAQE X-IronPort-AV: E=Sophos;i="5.04,758,1406584800"; d="scan'208";a="83998309" Received: from coaxial.xivilization.net ([178.63.18.39]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 20 Oct 2014 23:19:12 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xivilization.net; s=xiv; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Subject:To:From:Date; bh=yJGF+coZozsXLzL1j3mTKIKYAkcovKkWEWYZkKdgy60=; b=x7mmU5NbCQstNdjDQPO2nDJBtPkkD2bMmkUdWxjDa6sMwWYgrKB6Ec4dF4DDPN0wCcbmW7i5CtanKFiXDB6d+v0kcsq1ObsoWCO9JufPanQou+RlmkmhdTAETUo27Mml; Received: from aftr-88-217-180-41.dynamic.mnet-online.de ([88.217.180.41] helo=localhost.localdomain) by coaxial.xivilization.net with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1XgKM8-0002qg-JN for caml-list@inria.fr; Mon, 20 Oct 2014 23:19:08 +0200 Date: Mon, 20 Oct 2014 23:16:25 +0200 From: Marek Kubica To: caml-list@inria.fr Message-ID: <20141020231625.1fc5224a@xivilization.net> In-Reply-To: <543ED84D.4010302@etorok.net> References: <20141015112211.3e6bb721@xivilization.net> <543ED84D.4010302@etorok.net> X-Mailer: Claws Mail 3.10.1 (GTK+ 2.24.25; x86_64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Caml-list] Divide and print with precision Hello T=C3=B6r=C3=B6k, Sorry, I somehow missed your reply. On Wed, 15 Oct 2014 23:25:49 +0300 T=C3=B6r=C3=B6k Edwin wrote: > This uses Gmp.default_prec (120 bits by default) for the conversion. >=20 > 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: >=20 > let znum =3D Gmp.F.from_string_prec_base ~prec ~base:10 (Z.to_string > num) in let zden =3D Gmp.F.from_string_prec_base ~prec ~base:10 > (Z.to_string den) in let f =3D Gmp.F.div_prec ~prec znum zden in > Gmp.F.to_string_base_digits ~base:10 ~digits:0 f >=20 > 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: >=20 > let string_of_q_prec num den =3D > let znum =3D Gmp.Z.from_string (Z.to_string num) in > let zden =3D Gmp.Z.from_string (Z.to_string den) in > let f =3D 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 Thank you, that worked and outputs a lot of digits which is "good enough" for me. Great! For the record, here's the complete program: let prec =3D 1_000_000 let max_n =3D 205_211 let mode =3D Gmp.GMP_RNDN let euler_fraction n =3D let open Z in let numerator =3D ref one in let denominator =3D ref one in for i =3D 1 to n do numerator :=3D succ (!numerator * (of_int i)); denominator :=3D (of_int i) * !denominator; done; (!numerator, !denominator) let f () =3D let (num, den) =3D euler_fraction max_n in let znum =3D Gmp.Z.from_string @@ Z.to_string num in let zden =3D Gmp.Z.from_string @@ Z.to_string den in let euler =3D Gmp.FR.from_q_prec ~mode ~prec @@ Gmp.Q.from_zs znum zden in print_endline @@ Gmp.FR.to_string_base_digits ~mode ~base:10 ~digits:0 euler let () =3D 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. Neither do I, but I suppose I'd need a C stub to take the Z.t value (which if I understand Zarith correctly can be a GMP value, but for small values isn't) and convert it into a Gmp.Z.t. But for such a simple program and a constant amount of conversions, its not really worth it. regards, Marek