The Decimals
package provides basic data type and functions for arbitrary precision decimal floating point arithmetic in Julia.
Why is this needed? The following code in Julia gives an answer
julia> 0.1 + 0.2
0.30000000000000004
In words, the binary floating point arithmetics implemented in computers has finite resolution - not all real numbers (even within the limits) can be expressed exactly. While many scientific and engineering fields can handle this behavior, it is not acceptable in fields like finance, where it's important to be able to trust that $0.30 is actually 30 cents, rather than 30.000000000000004 cents.
See documentation.
Unlike another Julia package called DecFP
, which aims at implementing the IEEE 754-2008 standard introducing 32, 64, and 64-bit precisions (decimal32, decimal64 and decimal128, respectively), this package allows arbitrary precision. Note, however, that in comparision with DecFP
, which is is essentially a wrapper for a specialized Intel® Decimal Floating-Point Math Library, the present package is more computationally demanding. If more computational efficiency is demanded, functions from libmpdec
library can be called directly.
The closest equivalent (and inspiration) for the present package in Python is the standard built-in decimal
package, which is based on General Decimal Arithmetic Specification by IBM. Since version 3.3 of Python, it is actually libmpdec
/cdecimal
that is under the hood.
- What Every Programmer Should Know About Floating-Point Arithmetic!?! or Why don’t my numbers add up? floating-point-gui.de.
- Decimal Floating Point https://en.wikipedia.org/wiki/Decimal_floating_point
- IEEE 754-2008 revision https://en.wikipedia.org/wiki/IEEE_754-2008_revision
- 754-2008 - IEEE Standard for Floating-Point Arithmetic https://ieeexplore.ieee.org/document/4610935 Superseeded by 754-2019 - IEEE Standard for Floating-Point Arithmetic https://ieeexplore.ieee.org/document/8766229
- Intel® Decimal Floating-Point Math Library https://software.intel.com/en-us/blogs/2008/03/06/intel-decimal-floating-point-math-library/
- General Decimal Arithmetic Specification, IBM, Version 1.70 – 7 Apr 2009 http://speleotrove.com/decimal/decarith.html
mpdecimal
,libmpdec
- C/C++ library http://www.bytereef.org/mpdecimal/index.htmldecimal
- Decimal fixed point and floating point arithmetic, module for Python https://docs.python.org/2/library/decimal.html