OCaml bindings for MPFR.
mlmpfr provides 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:
- the
mpfr_prefix is ommited for all functions; mpfr_init*andmpfr_set*functions are not provided in order to implement these bindings with respect to the functional paradigm (and immutability). Consequently,mpfr_clear*functions are not provided too, and so, the garbage collector is in charge of memory management. See initilization functions;- functions managing the following C types are not supported:
unsigned long int,uintmax_t,intmax_t,float,long double,__float128,_Decimal64,_Decimal128,mpz_t,mpq_t, andmpf_t. Except formpfr_sqrt_uiandmpfr_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-bitint, 64-bitfloat, andstring) are supported. Thus, all functions named with*_[su]i*or*_d*are renamed here with*_int*or*_float*, respectively; - bindings to functions
mpfr_*printf,mpfr_*random*,mpfr_get_patches,mpfr_buildopt_*, and, macrosMPFR_VERSION*,mpfr_round_nearest_awayare not implemented. Everything else has been ported!
Building mlmpfr.4.1.1 depends on dune (an OCaml build system), ocaml version >= 4.04, and MPFR library version 4.1.1 (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
masterorrelease_411. Make sure that you have the proper MPFR library version installed on your system because mlmpfr won't check for it (seeutils/mpfr_version.c).
dune build @install @runtest
dune installUse --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 releases):
opam install mlmpfr
# `opam remove mlmpfr` to remove the package.Documentation depends on package odoc.
dune build @docthen, see _build/default/_doc/_html/index.html. An online version
is available here.
For example, let example.ml as follows:
module M = Mlmpfr
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.outwill result in:
$ ./a.out
9.449569463147377e-01You can also use dune with
$ dune exec examples/example.exe
9.449569463147377e-01Older 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.6Or, 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.opam packages mlmpfr.4.0.0 and mlmpfr.4.0.1 also exist and are suitable for
MPFR 4.0.0 and 4.0.1 versions.
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.