Skip to content

Commit 113de3d

Browse files
authored
Merge pull request #785 from cppalliance/basics
Expand the basics section
2 parents 139e355 + 6db7051 commit 113de3d

File tree

3 files changed

+86
-31
lines changed

3 files changed

+86
-31
lines changed

doc/decimal.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ https://www.boost.org/LICENSE_1_0.txt
1818
Matt Borland and Chris Kormanyos
1919

2020
include::decimal/overview.adoc[]
21+
include::decimal/basics.adoc[]
2122
include::decimal/api_reference.adoc[]
2223
include::decimal/generic_decimal.adoc[]
2324
include::decimal/decimal32.adoc[]

doc/decimal/basics.adoc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
----

doc/decimal/overview.adoc

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,3 @@ as well as emulated PPC64LE and STM32 using QEMU with the following compilers:
4040

4141
Tested on https://github.com/cppalliance/decimal/actions[Github Actions] and https://drone.cpp.al/cppalliance/decimal[Drone].
4242
Coverage can be found on https://app.codecov.io/gh/cppalliance/decimal[Codecov].
43-
44-
== Basic Usage
45-
46-
The entire library should be accessed using the convince header `<boost/decimal.hpp>`.
47-
A short example of the basic usage:
48-
49-
[source, c++]
50-
----
51-
#include <boost/decimal.hpp>
52-
#include <iostream>
53-
#include <iomanip>
54-
55-
int main()
56-
{
57-
using namespace boost::decimal;
58-
59-
// Outputs 0.30000000000000004
60-
std::cout << std::setprecision(17) << 0.1 + 0.2;
61-
62-
// Construct the two decimal values
63-
constexpr decimal64 a {1, -1}; // 1e-1 or 0.1
64-
constexpr decimal64 b {2, -1}; // 2e-1 or 0.2
65-
66-
// Outputs 0.30000000000000000
67-
std::cout << a + b << std::endl;
68-
69-
return 0;
70-
}
71-
72-
----
73-

0 commit comments

Comments
 (0)