Master | Develop | |
---|---|---|
Drone | ||
Github Actions | ||
Codecov | ||
Fuzzing |
Decimal is an implementation of IEEE-754:2008 decimal floating point numbers. See also [1].
The library is is header-only, and requires C++14. It is compatible through C++14, 17, 20, 23 and beyond.
Decimal is under active development and is not an official boost library.
This library is header only. It contains no other dependencies.
Simply #include
it and use it.
git clone https://github.com/cppalliance/decimal
cd decimal
mkdir build && cd build
cmake .. OR cmake .. -DCMAKE_INSTALL_PREFIX=/your/custom/path
cmake --install .
git clone https://github.com/cppalliance/decimal
cd decimal
vcpkg install decimal --overlay-ports=ports/decimal
git clone https://github.com/cppalliance/decimal
conan create decimal/conan --build missing
Boost.Decimal is tested natively on Ubuntu (x86_64, s390x, and aarch64), macOS (x86_64, and Apple Silicon), and Windows (x32 and x64); as well as emulated PPC64LE and STM32 using QEMU with the following compilers:
- GCC 7 and later
- Clang 6 and later
- Visual Studio 2017 and later
- Intel OneAPI DPC++
Decimal provides 3 IEEE-754 compliant types:
namespace boost {
namespace decimal {
class decimal32;
class decimal64;
class decimal128;
} //namespace decimal
} //namespace boost
and also 3 similar but non-compliant types with improved runtime performance:
namespace boost {
namespace decimal {
class decimal32_fast;
class decimal64_fast;
class decimal128_fast;
} //namespace decimal
} //namespace boost
These types operate like built-in floating point types.
They have their own implementations of the Standard-Library functions
(e.g. like those found in <cmath>
, <charconv>
, <cstdlib>
, etc.).
The entire library can be conveniently included with #include <boost/decimal.hpp>
Using the decimal types is simple.
#include <boost/decimal.hpp>
#include <iostream>
int main()
{
using boost::decimal::decimal32;
constexpr decimal32 a {2, -1}; // Constructs the number 0.2
constexpr decimal32 b {1, -1}; // Constructs the number 0.1
auto sum {a + b};
std::cout << sum << std::endl; // prints 0.3
const auto neg_a {2, -1, true}; // Constructs the number -0.2
sum += neg_a;
std::cout << sum << std::endl; // Prints 0.1
return 0;
}
This intuitive straightforwardness is the same when using Standard-Library
functions (such as STL functions, <cmath>
-like functions and the like).
#include <boost/decimal.hpp>
#include <cassert>
#include <cstring>
int main()
{
using namespace boost::decimal;
decimal64 val {-0.25}; // Construction from a double
val = abs(val); // DO NOT call std::abs
char buffer[256];
auto r_to = to_chars(buffer, buffer + sizeof(buffer) - 1, val);
assert(r_to); // checks std::errc()
*r_to.ptr = '\0';
decimal64 return_value;
auto r_from = from_chars(buffer, buffer + std::strlen(buffer), return_value);
assert(val == return_value);
return 0;
}
The complete documentation can be found at: https://cppalliance.org/decimal/decimal.html
[1] IEEE Computer Society. IEEE Standard for Floating-Point Arithmetic, Std. IEEE:754-2008, August 29, 2008 (doi:10.1109/IEEESTD.2008.4610935).