|
| 1 | +--- |
| 2 | +title: 'Boost.Decimal: A C++14 Library for Decimal Floating Point Arithmetic' |
| 3 | +tags: |
| 4 | + - C++ |
| 5 | +authors: |
| 6 | + - name: Matt Borland |
| 7 | + orcid: 0009-0005-8183-4254 |
| 8 | + equal-contrib: true |
| 9 | + affiliation: 1 |
| 10 | + - name: Christopher Kormanyos |
| 11 | + equal-contrib: true |
| 12 | + affiliation: 2 |
| 13 | +affiliations: |
| 14 | + - name: The C++ Alliance |
| 15 | + index: 1 |
| 16 | + - name: Independent Researcher, Germany |
| 17 | + index: 2 |
| 18 | +date: 04 February 2025 |
| 19 | +bibliography: paper.bib |
| 20 | +--- |
| 21 | + |
| 22 | +# Summary |
| 23 | + |
| 24 | +Boost.Decimal is a header-only, C++14 implementation of IEEE 754 decimal floating-point arithmetic types (`decimal32_t`, `decimal64_t` and `decimal128_t`), integrating seamlessly with the C++ Standard Template Library and other Boost Libraries [@boost]. |
| 25 | +The library is available at [https://github.com/boostorg/decimal](https://github.com/boostorg/decimal), and in the Boost distribution with most standard package managers starting from version 1.91. |
| 26 | + |
| 27 | +# Statement of need |
| 28 | + |
| 29 | +In the 2008 revision of IEEE 754 [@IEEE754:2008], decimal floating-point arithmetic was standardized. |
| 30 | +These types are critical in applications where decimal numbers must be represented exactly, such as financial software [@Cowlishaw:2003]. |
| 31 | +Binary floating-point representations cannot precisely store common decimal values like 0.1, leading to subtle rounding errors that accumulate over repeated calculations. |
| 32 | +Prior to Boost.Decimal, no cross-platform C++ library provided IEEE 754 decimal arithmetic or the associated types. |
| 33 | + |
| 34 | +# State of the field |
| 35 | + |
| 36 | +The two main implementations in this space are produced by IBM [@ibm_libdfp] and Intel [@intel_dfp], with the latter representing the industry standard. |
| 37 | +These libraries have two shortcomings that Boost.Decimal aims to address. First, both libraries are fundamentally C libraries. |
| 38 | +This can make integration into modern C++ applications awkward, and they lack support for C++-specific functionality provided in the Standard Library. |
| 39 | +Second, neither library is cross-platform; for instance, neither library supports ARM architectures. |
| 40 | +Boost.Decimal addresses both of these shortcomings. |
| 41 | + |
| 42 | +# Software design |
| 43 | + |
| 44 | +Boost.Decimal's design philosophy emphasizes ease of use, portability, and performance. |
| 45 | + |
| 46 | +To make the library easy to consume, it requires only C++14 and is header-only with zero external dependencies. |
| 47 | +Users can simply clone the repository, add `#include <boost/decimal.hpp>`, and begin using the library, even with older toolchains such as `clang++-6`. |
| 48 | +Portability is a primary design concern, so we test the library on all three major operating system families (Linux, Windows, and macOS) across a variety of architectures including: x86-32, x86-64, ARM32, ARM64, S390X, and PPC64LE. |
| 49 | +Our continuous integration pipeline ensures consistent numerical results across all supported platforms. |
| 50 | +Performance is also a priority, both in absolute terms and relative to existing libraries. |
| 51 | +[Extensive benchmarking](https://www.boost.org/doc/libs/develop/libs/decimal/doc/html/overview.html) demonstrates that Boost.Decimal outperforms both the Intel and IBM libraries in many operations across a variety of platforms. |
| 52 | +Benchmark results and methodology are available in the repository documentation. |
| 53 | + |
| 54 | +A minimal usage example showing the representation difference between decimal and binary floating point arithmetic: |
| 55 | + |
| 56 | +```c++ |
| 57 | +#include <boost/decimal.hpp> // This includes the entire decimal library |
| 58 | +#include <iostream> |
| 59 | +#include <iomanip> |
| 60 | + |
| 61 | +int main() |
| 62 | +{ |
| 63 | + using namespace boost::decimal::literals; // The literals are in their own namespace like std::literals |
| 64 | + |
| 65 | + // First we show the result of 0.1 + 0.2 using regular doubles |
| 66 | + std::cout << std::fixed << std::setprecision(17) |
| 67 | + << "Using doubles:\n" |
| 68 | + << "0.1 + 0.2 = " << 0.1 + 0.2 << "\n\n"; |
| 69 | + |
| 70 | + // Construct the two decimal values |
| 71 | + // We construct using the literals defined by the library |
| 72 | + constexpr boost::decimal::decimal64_t a {0.1_DD}; |
| 73 | + constexpr boost::decimal::decimal64_t b {0.2_DD}; |
| 74 | + |
| 75 | + // Now we display the result of the same calculation using decimal64_t |
| 76 | + std::cout << "Using decimal64_t:\n" |
| 77 | + << "0.1_DD + 0.2_DD = " << a + b << std::endl; |
| 78 | +} |
| 79 | +``` |
| 80 | +And the expected output is: |
| 81 | +``` |
| 82 | +Using doubles: |
| 83 | +0.1 + 0.2 = 0.30000000000000004 |
| 84 | +
|
| 85 | +Using decimal64_t: |
| 86 | +0.1_DD + 0.2_DD = 0.3000000000000000 |
| 87 | +``` |
| 88 | + |
| 89 | +# Research Impact Statement |
| 90 | + |
| 91 | +Boost.Decimal enables reproducible numerical research in domains where decimal precision is essential. |
| 92 | +The library's cross-platform consistency ensures that computational results are identical regardless of hardware architecture, a critical property for scientific reproducibility. |
| 93 | + |
| 94 | +Quantitative finance research frequently involves calculations on currency values, interest rates, and transaction amounts that are inherently decimal. |
| 95 | +Binary floating-point approximations can introduce systematic biases in Monte Carlo simulations, risk calculations, and backtesting frameworks. |
| 96 | +Boost.Decimal allows researchers to eliminate these artifacts and produce results that match real-world financial systems. |
| 97 | + |
| 98 | +Researchers investigating floating-point error accumulation can use Boost.Decimal as a reference implementation to compare against binary floating-point results. |
| 99 | +This facilitates studies on when decimal arithmetic provides meaningful accuracy improvements over binary representations. |
| 100 | +Additionally, unlike existing implementations, Boost.Decimal runs on ARM and other non-x86 architectures, enabling research on decimal arithmetic performance and applications in embedded systems and other platforms. |
| 101 | + |
| 102 | +# AI usage disclosure |
| 103 | + |
| 104 | +No generative AI tools were used in the development of this software, the writing |
| 105 | +of this manuscript, or the preparation of supporting materials. |
| 106 | + |
| 107 | +# Acknowledgements |
| 108 | + |
| 109 | +We acknowledge The C++ Alliance for sponsoring the development of this work. |
| 110 | +The library underwent peer review in January and October 2025 by domain experts before acceptance into the Boost library collection. |
| 111 | +We thank all of these reviewers for their time and contributions. |
| 112 | + |
| 113 | +# References |
0 commit comments