mpdecimal is a package for correctly-rounded arbitrary precision decimal floating point arithmetic.

libmpdec - C library

libmpdec is a complete implementation of the General Decimal Arithmetic Specification. The specification, written by Mike Cowlishaw from IBM, defines a general purpose arbitrary precision data type together with rigorously specified functions and rounding behavior. As described in the scope section of the specification, libmpdec will - with minor restrictions - also conform to the IEEE 754-2008 Standard for Floating-Point Arithmetic, provided that the appropriate context parameters are set.

libmpdec is written in C, but the header files are prepared for use with a C++ compiler.

Starting from Python-3.3, libmpdec is the basis for Python’s decimal module.

libmpdec++ - C++ library

libmpdec++ is a complete implementation of the General Decimal Arithmetic Specification. libmpdec++ is mostly a header library around libmpdec’s C functions.

The library frees users from manual memory management and has an easy API with inline operators similar to the one in Python’s decimal module. Like Python’s decimal module, libmpdec++ has a thread local context for inline operators and other functions that use the implicit context.

In benchmarks the speed is close to libmpdec (about 4% slower due to the copying, destructor overhead and the thread local context).

Architectures and compilers

Both libmpdec and libmpdec++ are regularly tested on Linux, FreeBSD, NetBSD, OpenBSD, Windows, macOS, AIX and Solaris.

Complete tests are run on the following architectures: x86_64, i386, aarch64, ppc64, ppc64le, chrp, sparc64, sparc32, mips64, mips32 and risc-v.

Supported C compilers: gcc, clang, icc, xlc and cl.exe.

Supported C++ compilers: g++, clang++, icpc, and cl.exe.

cdecimal - Python module (2.7 - 3.2)

cdecimal has been included in Python-3.3 and later, just “import decimal”.

cdecimal is a fast drop-in replacement for the decimal module in Python’s standard library. Both modules provide complete implementations of the General Decimal Arithmetic Specification.

Typical performance gains are between 30x for I/O heavy benchmarks and 80x for numerical programs. In a database benchmark, cdecimal exhibits a speedup of 12x over

  decimal cdecimal speedup
pi 42.75s 0.58s 74x
telco 172.19s 5.68s 30x
psycopg 3.57s 0.29s 12x

All Python versions from 2.5 up to 3.2 are supported. For the few remaining differences, read the cdecimal documentation.