Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] Divide and print with precision
@ 2014-10-15  9:22 Marek Kubica
  2014-10-15 10:41 ` Drup
  2014-10-15 20:25 ` Török Edwin
  0 siblings, 2 replies; 5+ messages in thread
From: Marek Kubica @ 2014-10-15  9:22 UTC (permalink / raw)
  To: caml users

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-10-20 21:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-15  9:22 [Caml-list] Divide and print with precision Marek Kubica
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox