package decimal

  1. Overview
  2. Docs
Arbitrary-precision floating-point decimal library

Install

Dune Dependency

Authors

Maintainers

Sources

decimal-0.4.0.tbz
sha256=c7f642ca90aa23f9b125a8647509ce508e160592245c3b605a0407656f7a0af2
sha512=909f0dd7bacbbbb3fa17a79c18794520495f39cc3e8ade2545cedd8ec311e386696b9d62a095d9a4ed6fbc69d7286e6e138719b9d08dcb3d8f775c36acd18caf

Description

Arbitrary-precision floating-point decimal library ported from the Python decimal module.

Published: 26 Apr 2023

README

decimal

Arbitrary-precision floating-point decimal type implemented in OCaml. Ported from Python decimal module. It uses Zarith to do biginteger arithmetic.

Opam: https://opam.ocaml.org/packages/decimal/

License

This package is licensed under the Python Software Foundation License v2, for the sake of simplicity, as it is a derived work of the Python decimal module.

Examples

$ opam install decimal # if trying out
# (* if trying out: *)
  #require "decimal";;
# (* for convenience *)
  module D = Decimal
  let i = D.of_int
  let s = D.of_string;;
# (* tell the REPL how to display decimals *)
  #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
# (* round to decimal places: *)
  D.(round ~n:2 (of_int 22 / of_int 7));;
- : D.t = 3.14

Dev

Build:

dune build

Try in REPL:

dune utop

Tests

The test runner source is in test/decimal_test.ml. It parses and runs the test cases in test/data/. I am adding test case data files from the Python snapshot as I go.

Run current tests:

dune test

Note that, some of the tests don't make sense for the OCaml port and have thus been deleted. If you ever need to update to new versions of the test files, you can apply the changes as patches after re-dowloading the relevant *.decTest files:

git show 07074859567e936b8d170aba5ef58889a4d9d467 | git apply
git show ae0196377fb7a99db7f198f2fb242e6a2fe4541e | git apply

Dependencies (3)

  1. zarith >= "1.10" & < "2.0.0"
  2. ocaml >= "4.08.0"
  3. dune >= "2.7"

Dev Dependencies (3)

  1. odoc with-doc
  2. angstrom >= "0.15.0" & < "1.0.0" & with-test
  3. alcotest >= "1.5.0" & < "2.0.0" & with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.