mpdecimal

mpdecimal is a package for correctly-rounded arbitrary precision decimal floating point arithmetic. It provides a C/C++ library as well as a Python module.

libmpdec - C/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. It is intended to be cross platform and has been tested on 64/32-bit Linux, 64/32-bit FreeBSD and OpenBSD, 64/32-bit Windows, 64-bit AIX, 64/32-bit OpenSolaris and 32-bit Debian Mips.

cdecimal - Python module

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.

cdecimal has been integrated into Python 3.3, where it is the default implementation. It is transparently available as "import decimal", i.e. the name "cdecimal" is no longer in use.

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

  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.