Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Marek Kubica <marek@xivilization.net>
To: caml users <caml-list@inria.fr>
Subject: [Caml-list] Divide and print with precision
Date: Wed, 15 Oct 2014 11:22:11 +0200	[thread overview]
Message-ID: <20141015112211.3e6bb721@xivilization.net> (raw)

Hello,

I've got this short program here:

let prec = 1_000_000
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
  let euler = Gmp.F.div_prec ~prec znum zden in
  print_endline @@ to_string euler

let () = f ()

It just computes e, using Zarith and Gmp. While Zarith works splendid,
I am having problems getting the division to work. I mean, it does
work, but I don't know how precise it is, because I can't get mlgmp to
print the 1_000_000 digits. From what I read in the GMP docs, 0 digits
means "whatever precision is available" but even if I explicitly
specify 1000 digits or so, the output is always
"2.718281828459045235360287471352662497757E0".

What can I do to get longer output? I don't insist on mlgmp (in fact,
it leaks memory like crazy, before I used Zarith I used mlgmp which
used 12 GB of RAM before I killed it), I just want some way to get a
base-10 floating point representation of my number.

Hope someone knows a solution.

regards,
Marek

             reply	other threads:[~2014-10-15  9:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-15  9:22 Marek Kubica [this message]
2014-10-15 10:41 ` Drup
2014-10-15 11:13   ` Marek Kubica
2014-10-15 20:25 ` Török Edwin
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=20141015112211.3e6bb721@xivilization.net \
    --to=marek@xivilization.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