|
| 1 | +//// |
| 2 | +Copyright 2025 Matt Borland |
| 3 | +Distributed under the Boost Software License, Version 1.0. |
| 4 | +https://www.boost.org/LICENSE_1_0.txt |
| 5 | +//// |
| 6 | + |
| 7 | +[#basics] |
| 8 | += Basic Usage |
| 9 | +:idprefix: basics_ |
| 10 | + |
| 11 | +== Construction of Decimal Types |
| 12 | + |
| 13 | +Every decimal type can be constructed in a few ways: |
| 14 | + |
| 15 | +1) The parameters to the constructor are like so: |
| 16 | + |
| 17 | +[source, c++] |
| 18 | +---- |
| 19 | +template <typename T, typename T2> |
| 20 | +constexpr decimal32(T coeff, T2 exp, bool sign = false) noexcept; |
| 21 | +---- |
| 22 | + |
| 23 | +Where types `T` and `T2` are integral types (signed and unsigned are both allowed). |
| 24 | +Lastly the sign follows the convention of `signbit` where `false` is positive and `true` is negative. |
| 25 | +If both a negative coefficient and a sign are passed then the resulting decimal number will be negative. |
| 26 | +The final number constructed is in the form (sign || coeff < 0 ? -1 : 1) x abs(coeff) x 10^exp. |
| 27 | + |
| 28 | +[souce, c++] |
| 29 | +---- |
| 30 | +boost::decimal::decimal32 a {1, 0}; // constructs 1 |
| 31 | +boost::decimal::decimal32 b {-2, 0}; // constructs -2 |
| 32 | +boost::decimal::decimal32 c {2, 0, true}; // Also constructs -2 |
| 33 | +boost::decimal::decimal32 d {-2, 0, true}; // Also constructs -2 |
| 34 | +boost::decimal::decimal32 e {5, 5}; // constructs 5x10^5 |
| 35 | +boost::decimal::decimal32 f {1234, -3} // constructs 1.234 or 1234x10^-3 |
| 36 | +---- |
| 37 | + |
| 38 | +2) A decimal number can be explicitly or implicitly constructed from an integer. |
| 39 | +For example: |
| 40 | + |
| 41 | +[source, c++] |
| 42 | +---- |
| 43 | +boost::decimal::decimal64 g = 1; |
| 44 | +boost::decimal::decimal32 h {-4}; |
| 45 | +---- |
| 46 | + |
| 47 | +3) A decimal number can only be explicitly constructed from a floating point type. |
| 48 | +For example: |
| 49 | + |
| 50 | +[source, c++] |
| 51 | +---- |
| 52 | +boost::decimal::decimal128 pi {3.14}; |
| 53 | +---- |
| 54 | + |
| 55 | +NOTE: Due to the differences in decimal and binary floating point numbers there may be a difference in the resulting representation in decimal format, and thus it is not recommended to construct from binary floating point numbers |
| 56 | + |
| 57 | +== Using the Library |
| 58 | + |
| 59 | +The entire library should be accessed using the convince header `<boost/decimal.hpp>`. |
| 60 | +A short example of the basic usage: |
| 61 | + |
| 62 | +[source, c++] |
| 63 | +---- |
| 64 | +#include <boost/decimal.hpp> |
| 65 | +#include <iostream> |
| 66 | +#include <iomanip> |
| 67 | +
|
| 68 | +int main() |
| 69 | +{ |
| 70 | + using namespace boost::decimal; |
| 71 | +
|
| 72 | + // Outputs 0.30000000000000004 |
| 73 | + std::cout << std::setprecision(17) << 0.1 + 0.2 << std::endl; |
| 74 | +
|
| 75 | + // Construct the two decimal values |
| 76 | + constexpr decimal64 a {1, -1}; // 1e-1 or 0.1 |
| 77 | + constexpr decimal64 b {2, -1}; // 2e-1 or 0.2 |
| 78 | +
|
| 79 | + // Outputs 0.30000000000000000 |
| 80 | + std::cout << a + b << std::endl; |
| 81 | +
|
| 82 | + return 0; |
| 83 | +} |
| 84 | +
|
| 85 | +---- |
0 commit comments