OCaml bindings for MPFR.
mpfr_float, an immutable data structure that contains a
mpfr_t number, as well as an optional ternary value, as provided by (and
described in) the MPFR library.
A few distinctions are made from the original C library:
mpfr_prefix is ommited for all functions;
mpfr_set*functions are not provided in order to implement
these bindings with respect to the functional paradigm (and immutability).
mpfr_clear*functions are not provided too, and so, the
garbage collector is in charge of memory management. See initilization
functions managing the following C types are not supported:
unsigned long int,
mpf_t. Except for
mpfr_fac_uiwhich are partially supported in the range of
the positive values of an OCaml signed integer. In fact, only the OCaml native
types (signed 64-bit
string) are supported. Thus,
all functions named with
*_d*are renamed here with
bindings to functions
mpfr_buildopt_*, and, macros
not implemented. Everything else has been ported!
build and install
Building mlmpfr.4.1.0 depends on dune (an
OCaml build system), ocaml version >= 4.04, and MPFR library version 4.1.0
(see footnote for building older mlmpfr releases). Basically you just need to
install mlmpfr via the opam package manager, which will triggers all the
dependencies (such as dune for example).
From sources (github repo or with latest
releases): in mlmpfr main
directory, on branch
release_410. Make sure that you have the
proper MPFR library version installed on your system because mlmpfr won't
check for it (see
dune build @install @runtest dune install
--prefix <path> to set another path (the default path is the parent of the
directory where ocamlc was found).
From opam, targeting latest release (see
opam infoto list available
opam install mlmpfr # `opam remove mlmpfr` to remove the package.
Documentation depends on package odoc.
dune build @doc
_build/default/_doc/_html/index.html. An online version
is available here.
For example, let example.ml as follows:
module M = Mpfr let _ = let op = M.make_from_float (~-. 1. /. 3.) in Printf.printf "%s\n" (M.get_formatted_str (M.cos op))
Compile the above code with:
$ ocamlfind ocamlc -package mlmpfr -linkpkg example.ml -o a.out
will result in:
$ ./a.out 9.449569463147377e-01
You can also use dune with
$ dune exec examples/example.exe 9.449569463147377e-01
Note: install an older release of mlmpfr
Older releases of mlmpfr (3.1.6, 4.0.0, and 4.0.1) depend on
oasis, an obsolete build system replaced
by dune since
mlmpfr.4.0.2. Use opam if you need to install an older release
of mlmpfr, as for example:
opam install mlmpfr.3.1.6
Or, you can build it from the sources, as for example from branch release_316:
oasis setup ./configure --enable-tests make make test make install # `make uninstall` to remove mlmpfr library.
mlmpfr.4.0.1 also exist and are suitable for
MPFR 4.0.0 and 4.0.1 versions.
Note: build examples with an older release of mlmpfr
You'll need to link with
-cclib -lmpfr (along with
ocamlopt) if you are
using released versions of mlmpfr (as the ones provided by opam), since mlmpfr
wasn't linked with mpfr by default before.