Giter Site home page Giter Site logo

mlmpfr's Introduction

master

mlmpfr

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* and mpfr_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, and mpf_t. Except for mpfr_sqrt_ui and mpfr_fac_ui which 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 int, 64-bit float, and string) 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, macros MPFR_VERSION*, mpfr_round_nearest_away are not implemented. Everything else has been ported!

build and install

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 master or release_411. Make sure that you have the proper MPFR library version installed on your system because mlmpfr won't check for it (see utils/mpfr_version.c).
dune build @install @runtest
dune install

Use --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 info to list available releases):
opam install mlmpfr
# `opam remove mlmpfr` to remove the package.

documentation

Documentation depends on package odoc.

dune build @doc

then, see _build/default/_doc/_html/index.html. An online version is available here.

usage

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.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.

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.

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.

mlmpfr's People

Contributors

bobot avatar jamesjer avatar mseri avatar thvnx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mlmpfr's Issues

Improve external dependency management

Hello,

Currently installing mlmpfr on a fresh ubuntu 20.04 fails because opam depext mlmpfr installs the libmpfr-dev 4.0.2-1 system package (the latest version available on the official repository), but opam will try to install the 4.1.0 release of mlmpfr which requires a higher version...

I think you can make the installation of mlmpfr easier by changing the mlmpfr.opam config file.
The current config file contains the following lines :

depexts: [
  ["libmpfr-dev"] {os-family = "debian"}
]

You could explicit the need for a specific version of libmpfr. For instance the 4.0.2 release of mlmpfr could contain these lines instead :

depexts: [
  ["libmpfr-dev=4.0.2-1"] {os-family = "debian"}
]

Sincerely,

Linking of the library

I'm wondering why the -lmpfr is not written in the cmxa and cma in Extra C object files, only the linking of the stubs is written -lmlmpfr_stubs. To compare zarith indicates -lzarith -lgmp which allows to link zarith like any other ocaml library.

Cf Documentation sur -a

Examples fail to compile with latest dune

File "examples/dune", line 2, characters 16-24:
2 |  (names example rndo-add sample)
                    ^^^^^^^^
Error: "rndo-add" is an invalid module name.
Module names must be non-empty and composed only of the following characters:
'A'..'Z', 'a'..'z', '_', ''' or '0'..'9'.
Hint: rndo_add would be a correct module name

dune build @doc fails

with

File "../../src/.mlmpfr.objs/byte/mpfr.odoc":
13
Couldn't find the following modules:
14
  Stdlib

Version field warning in opam

[WARNING] At /home/luca/.opam/repo/default/packages/mlmpfr/mlmpfr.4.0.0/opam:3:0:
          This file is for version '4.0.0' but its 'version:' field advertises '4.0.x'.

I don't know much about opam file syntax but I guess '4.0.x' is not a valid syntax and the version field should contain an exact version number?

Opam version is limited to MPFR 4.1.1

Hi. The latest version on opam is still tailored to MPFR 4.1.1. I know master is now compatible with the latest versions, would it be possible to release it on opam please ?

Polymorphic comparison segfaults

According the documentation :

Operators =, <>, >, <, >=, and <= are supported. They are based on mpfr_cmp.

However, the following code segfauts on my laptop :

let one = Mpfr.make_from_int 1 in
one = one
;;

Comparison based on Mpfr.cmp works.

Compatibility with MPFR 4.2.0

Would it be possible to make this package compatible with MPFR 4.2.0 ? I've seen some commits pushing in that direction but the compatibility test still rejects other versions than 4.1.1, which is problematic on an updated ArchLinux for example.

Test failures with mpfr 4.2.1

Tests fail with mlmpfr git HEAD + mpfr 4.2.1:

File "testsuite/basicarithmeticfunctions.expected", line 1, characters 0-0:
(cd _build/default && /usr/bin/diff -u testsuite/basicarithmeticfunctions.expected testsuite/basicarithmeticfunctions.output)
--- testsuite/basicarithmeticfunctions.expected	2023-08-28 10:38:26.151053076 -0600
+++ testsuite/basicarithmeticfunctions.output	2023-08-28 10:38:26.347049731 -0600
@@ -42,11 +42,11 @@
 -1.0000000000000000e+01
 -1.3333333333333332e+01
 -1.3333333333333332e+01
--nan
--nan
+nan
+nan
 -1.1006424162982089e+00
--nan
--nan
+nan
+nan
 1.0000000000000000e+00
 1.3333333333333333e+00
 1.3333333333333333e+00
@@ -94,7 +94,7 @@
 0.0000000000000000e+00
 -0.0000000000000000e+00
 -0.0000000000000000e+00
--nan
+nan
 -0.0000000000000000e+00
 -0.0000000000000000e+00
 -0.0000000000000000e+00
File "testsuite/integerandremainderrelatedfunctions.expected", line 1, characters 0-0:
(cd _build/default && /usr/bin/diff -u testsuite/integerandremainderrelatedfunctions.expected testsuite/integerandremainderrelatedfunctions.output)
--- testsuite/integerandremainderrelatedfunctions.expected	2023-08-28 10:38:26.145053179 -0600
+++ testsuite/integerandremainderrelatedfunctions.output	2023-08-28 10:38:26.349049697 -0600
@@ -87,8 +87,8 @@
 -0.0000000000000000e+00 Correct
 -0.0000000000000000e+00 Correct
 -0.0000000000000000e+00 Correct -0.0000000000000000e+00 Correct
--nan Correct
--nan Correct 0
--nan Correct
--nan Correct 0
+nan Correct
+nan Correct 0
+nan Correct
+nan Correct 0
 true
Running[75]: (cd _build/default/testsuite && ./specialfunctions.exe) > _build/default/testsuite/specialfunctions.output
File "testsuite/specialfunctions.expected", line 1, characters 0-0:
(cd _build/default && /usr/bin/diff -u testsuite/specialfunctions.expected testsuite/specialfunctions.output)
--- testsuite/specialfunctions.expected	2023-08-28 10:38:26.345049765 -0600
+++ testsuite/specialfunctions.output	2023-08-28 10:38:26.685043962 -0600
@@ -95,14 +95,14 @@
 4.2510215777136313e+00 Greater
 -1.0288723164281641e+00 Lower
 -2.4202942695518675e-01 Greater
--nan Correct
--nan Correct
+nan Correct
+nan Correct
 -9.2729521800161219e-01 Greater
--nan Correct
--nan Correct
+nan Correct
+nan Correct
 -4.4275085295129979e-01 Greater
--nan Correct
--nan Correct
+nan Correct
+nan Correct
 -2.9516723530086653e-01 Greater
 -1.4959364790841299e+00 Lower
 -7.1425705559317487e-01 Lower
@@ -114,11 +114,11 @@
 4.9294290213530861e-01 Lower
 -5.6656088161381446e-01 Greater
 -1.1493438269617247e+00 Lower
--nan Correct
+nan Correct
 -1.0986122886681096e+00 Greater
--nan Correct
--nan None
--nan Correct
+nan Correct
+nan None
+nan Correct
 -7.3640286188427317e-01 Greater
 -1.2866430426774744e-01 Lower
 -1.6228128139692766e+00 Greater
@@ -219,7 +219,7 @@
 4.2022653690536244e+14 Greater
 1.7659034231947393e+29 Lower
 -3.1184149000508982e+58 Greater
--nan Correct
+nan Correct
 1.1304915351788846e+01 Greater
 2.7068264745934528e-01 Lower
 6.9314718055994529e-01 Lower
@@ -345,7 +345,7 @@
 1.0000000000000000e+00 Correct
 -inf Correct
 -inf Correct
--nan Correct
+nan Correct
 -0.0000000000000000e+00 Correct
 -0.0000000000000000e+00 Correct
 1.0000000000000000e+00 Correct

This change (from the ChangeLog) is probably the cause:

2023-08-15 Vincent Lefevre [email protected]

Formatted output functions: fixed ‘+’/space flags on NaN and Inf.

This is about the documentation and behavior of the mpfr_printf,
etc. functions on MPFR numbers (mpfr_t) only, i.e. with the ‘R’
type specifier.

The ‘+’ and space flags were ignored on NaN and Inf. While this
was loosely documented as such (without an explicit mention of
these flags), the GNU MPFR manual also says that the flags have
the same meaning as for the standard ‘printf’ function. So this
was contradictory and regarded as a bug.

Behaving like the ISO C standard should give less surprise, and
this is probably what is expected (better for alignment purpose).

This was partly discussed at
https://sympa.inria.fr/sympa/arc/mpfr/2023-03/msg00003.html
(only for NaN and the ‘+’ flag at that time).

  • doc/mpfr.texi: corrected the documentation.
  • src/vasprintf.c: fixed (the code is now simpler since the rule
    is the same in all cases: real and non-real values).
  • tests/tsprintf.c: grouped all the tests of NaN and Inf, and
    added much more tests (by constructing the format string in
    nested loops instead of just a few hardcoded cases).

mlgmpidl and mlmpfr defines the same module

The module mpfr is defined in both libraries, so we can't link both of them at the same time (e.g Apron and Why3). It is possible to prefix one of them and still provide a backward compatible version that automatically make the translation. But @nberth and @thvnx are you willing to fix this incompatibility?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.