From: "Török Edwin" <edwin+ml-ocaml@etorok.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Divide and print with precision
Date: Wed, 15 Oct 2014 23:25:49 +0300 [thread overview]
Message-ID: <543ED84D.4010302@etorok.net> (raw)
In-Reply-To: <20141015112211.3e6bb721@xivilization.net>
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
next prev parent reply other threads:[~2014-10-15 20:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-15 9:22 Marek Kubica
2014-10-15 10:41 ` Drup
2014-10-15 11:13 ` Marek Kubica
2014-10-15 20:25 ` Török Edwin [this message]
2014-10-20 21:16 ` Marek Kubica
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=543ED84D.4010302@etorok.net \
--to=edwin+ml-ocaml@etorok.net \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox