Hi, I’ve been working on a
decimal package: https://github.com/yawaramin/ocaml-decimal (arbitrary-precision floating-point decimals).
I’ve published a very early preview on opam: http://opam.ocaml.org/packages/decimal/
You can try it out:
$ opam install decimal $ utop # #require "decimal";; # module D = Decimal let i = D.of_int let s = D.of_string;; # #install_printer D.pp;; # D.(s "0.1" + s "0.2");; - : D.t = 0.3 # (* default precision is 32 *) D.(i 1 / i 3);; - : D.t = 0.33333333333333333333333333333333
The module is basically a port of the Python decimal module, with some simplifications like, I’m ignoring different types of NaN like negative NaN, quiet NaN, signalling NaN, and just treating them all as NaN. Otherwise I aim to have the same functionality.
What I need help with mainly is porting the Python
decimal unit tests: https://github.com/python/cpython/tree/23831a7a90956e38b7d70304bb6afe30d37936de/Lib/test/decimaltestdata . They are written in a plain-text DSL consisting of test cases like:
addx6324 add 0.12 0.01 -> 0.13
And I assume they have an interpreter somewhere that parses and runs these tests as standard Python unit tests.
What I would like to do is to port these tests into plain OCaml. The above test case could look like (e.g. Alcotest):
check string "addx6324" "0.13" D.(to_string (s "0.12" + s "0.01"))
The test DSL has a few more features, like comments and setting and checking flags, but not too much more complex. So I would really appreciate some help with porting them. I was thinking good old regular expressions to do the conversions, but I’m open to suggestions.