Skip to content

Commit c795e7e

Browse files
authored
Merge pull request #1242 from cppalliance/1241
Improve formatting of examples, better section separation, and point README to examples
2 parents 51b8ddb + 6e75d0b commit c795e7e

File tree

4 files changed

+81
-147
lines changed

4 files changed

+81
-147
lines changed

README.md

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,8 @@
1010

1111
---
1212

13-
Decimal is an implementation of IEEE-754:2008 decimal floating point numbers.
14-
See also [1].
15-
16-
The library is header-only, and requires C++14.
17-
It is compatible through C++14, 17, 20, 23 and beyond.
18-
19-
# Notice
20-
21-
Decimal is under active development and is not an official boost library.
13+
Boost.Decimal is an implementation of [IEEE 754](https://standards.ieee.org/ieee/754/6210/) and [ISO/IEC DTR 24733](https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2849.pdf) Decimal Floating Point numbers.
14+
The library is header-only, has no dependencies, and requires C++14.
2215

2316
# How To Use The Library
2417

@@ -95,78 +88,11 @@ class decimal_fast128_t;
9588
These types operate like built-in floating point types.
9689
They have their own implementations of the Standard-Library functions
9790
(e.g. like those found in `<cmath>`, `<charconv>`, `<cstdlib>`, etc.).
98-
9991
The entire library can be conveniently included with `#include <boost/decimal.hpp>`
10092

101-
Using the decimal types is simple.
102-
103-
```cpp
104-
#include <boost/decimal.hpp>
105-
#include <iostream>
106-
107-
int main()
108-
{
109-
using boost::decimal::decimal32_t;
110-
111-
constexpr decimal32_t a {2, -1}; // Constructs the number 0.2
112-
constexpr decimal32_t b {1, -1}; // Constructs the number 0.1
113-
auto sum {a + b};
114-
115-
std::cout << sum << std::endl; // prints 0.3
116-
117-
const auto neg_a {2, -1, true}; // Constructs the number -0.2
118-
119-
sum += neg_a;
120-
121-
std::cout << sum << std::endl; // Prints 0.1
122-
123-
return 0;
124-
}
125-
```
126-
127-
This intuitive straightforwardness is the same when using Standard-Library
128-
functions (such as STL functions, `<cmath>` functions and the like).
129-
130-
```cpp
131-
#include <boost/decimal.hpp>
132-
#include <cassert>
133-
#include <cstring>
134-
135-
int main()
136-
{
137-
using namespace boost::decimal;
138-
139-
decimal64_t val {-0.25}; // Construction from a double
140-
val = abs(val); // DO NOT call std::abs
141-
142-
char buffer[256];
143-
auto r_to = to_chars(buffer, buffer + sizeof(buffer) - 1, val);
144-
assert(r_to); // checks std::errc()
145-
*r_to.ptr = '\0';
146-
147-
decimal64_t return_value;
148-
auto r_from = from_chars(buffer, buffer + std::strlen(buffer), return_value);
149-
150-
assert(val == return_value);
151-
152-
return 0;
153-
}
154-
```
93+
Using the decimal types is simple and can be learned by [example](https://develop.decimal.cpp.al/decimal/examples.html).
94+
Their usage closely resembles that of built-in binary floating point types by design.
15595

15696
# Full Documentation
15797

15898
The complete documentation can be found at: https://develop.decimal.cpp.al
159-
160-
## References
161-
162-
- Michael F. Cowlishaw [_Floating-Point: Algorism(sic) for Computers_](https://www.cs.tufts.edu/~nr/cs257/archive/mike-cowlishaw/decimal-arith.pdf_Decimal), Proceedings of the 16th IEEE Symposium on Computer Arithmetic, 2003
163-
164-
- Donald E. Knuth, _The Art of Computer Programming Volume 2 Seminumerical Algorithms_, 3rd edition, 1998
165-
166-
- Jean-Michel Muller, _Elementary Functions_, 3rd edition, 2010
167-
168-
- Jean-Michel Muller, et. al., _Handbook of Floating-Point Arithmetic_, 2000
169-
170-
- John F. Hart, et. al., _Computer Approximations_, 1968
171-
172-
- IEEE, _IEEE Standard for Floating-Point Arithmetic_, 2019

doc/modules/ROOT/nav.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
* xref:overview.adoc[]
22
* xref:basics.adoc[]
33
* xref:examples.adoc[]
4-
** xref:examples.adoc#examples_construction[Construction]
5-
** xref:examples.adoc#examples_promotion[Type Promotion]
4+
** xref:examples.adoc#examples_construction[Basic Construction]
5+
** xref:examples.adoc#examples_promotion[Promotion and Mixed Decimal Arithmetic]
66
** xref:examples.adoc#examples_charconv[`<charconv>`]
77
** xref:examples.adoc#examples_generic_programming[Generic Programming]
88
** xref:examples.adoc#examples_literals_constants[Literals and Constants]
9-
** xref:examples.adoc#examples_finance[Financial Applications]
10-
** xref:examples.adoc#examples_boost_math[Boost.Math Integration]
119
** xref:examples.adoc#examples_format[Formatting]
1210
*** xref:examples.adoc#examples_fmt_format[pass:[{fmt}]]
1311
*** xref:examples.adoc#examples_std_format[`<format>`]
14-
** xref:examples.adoc#examples_print[`<print>`]
12+
*** xref:examples.adoc#examples_print[`<print>`]
13+
* xref:financial_examples.adoc[]
14+
** xref:financial_examples.adoc#examples_boost_math[Boost.Math Integration]
1515
* xref:api_reference.adoc[]
1616
** xref:api_reference.adoc#api_ref_types[Types]
1717
** xref:api_reference.adoc#api_ref_structs[Structs]

doc/modules/ROOT/pages/examples.adoc

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@ https://www.boost.org/LICENSE_1_0.txt
1111
All examples can be found in the library `examples/` folder as well.
1212

1313
[#examples_construction]
14-
== Basic construction
15-
16-
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/basic_construction.cpp[basic_construction.cpp]
17-
14+
== Basic Construction
15+
.This example demonstrates the basic use of the various constructors offered by the decimal types
16+
====
1817
[source, c++]
1918
----
2019
include::example$basic_construction.cpp[]
2120
----
2221

23-
The expected output from this example is:
24-
----
22+
.Expected Output
23+
....
2524
Val_1: 100
2625
Val_2: 100
2726
Val_3: -100
@@ -30,58 +29,60 @@ Underflow constructs zero
3029
NaN constructs NaN
3130
Values constructed from const char* and std::string are the same
3231
Can not construct from invalid string
33-
----
32+
....
33+
====
3434
3535
[#examples_promotion]
3636
== Promotion and Mixed Decimal Arithmetic
3737
38-
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/promotion.cpp[promotion.cpp]
39-
38+
.This example demonstrates the behaviors of promotion between types, and mixed decimal type arithmetic
39+
====
4040
[source, c++]
4141
----
4242
include::example$promotion.cpp[]
4343
----
4444

45-
Expected Output:
46-
----
45+
.Expected Output:
46+
....
4747
decimal32_t value (a): 5.2
4848
decimal64_t value (b): 3.9
4949
a is greater than b
5050
5.2 is less than 1e+385
5151
1e+385 is now less than inf
5252
The result of a + b is a decimal64_t: 9.1
53-
----
53+
....
54+
====
5455
5556
[#examples_charconv]
5657
== `<charconv>`
5758
58-
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/charconv.cpp[charconv.cpp]
59-
59+
.This example demonstrates the fundamentals of the `<charconv>` like functions provided by the library
60+
====
6061
[source, c++]
6162
----
6263
include::example$charconv.cpp[]
6364
----
6465

65-
Output:
66-
----
66+
.Expected Output:
67+
....
6768
Initial decimal: -7123450
6869
Value from string: 3.1415
6970
Value in scientific format: -7.12345e+06
7071
Value in scientific format with precision 20: -7.12345000000000000000e+06
71-
----
72+
....
73+
====
7274
7375
[#examples_generic_programming]
7476
== Generic Programming
75-
76-
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/adl.cpp[adl.cpp].
77-
77+
.This example demonstrates how to write generic code that accepts both built-in floating point types, and decimal floating point values from this library
78+
====
7879
[source, c++]
7980
----
8081
include::example$adl.cpp[]
8182
----
8283

8384
Expected Output:
84-
----
85+
....
8586
Float:
8687
sin(-0.5) = -0.479426
8788
-sin(0.5) = -0.479426
@@ -105,49 +106,28 @@ sin(-0.5) = -0.479426
105106
decimal128_t:
106107
sin(-0.5) = -0.479426
107108
-sin(0.5) = -0.479426
108-
----
109+
....
110+
====
109111

110112
[#examples_literals_constants]
111113
== Literals and Constants
112-
113-
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/literals.cpp[literals.cpp].
114-
114+
.This example demonstrates how to construct values using literals, and the usage of numerical constants that are provided by the library
115+
====
115116
[source, c++]
116117
----
117118
include::example$literals.cpp[]
118119
----
119120

120121
Expected Output:
121-
----
122+
....
122123
32-bit Pi: 3.141593
123124
64-bit Pi: 3.141592653589793
124125
32-bit UDL Pi: 3.141593
125126
Rounded UDL has the same value as the 32-bit constant
126127
64-bit UDL Pi: 3.141592653589793
127128
Rounded UDL has the same value as the 64-bit constant
128-
----
129-
130-
[#examples_finance]
131-
== Financial Applications
132-
133-
=== Simple Moving Average
134-
135-
In the examples folder there is a file named `moving_average.cpp`.
136-
This example shows how to parse historical stock data from a file and use it.
137-
This serves as a framework for other calculations for securities.
138-
139-
=== Currency Conversion
140-
In the examples folder there is a file named `currency_conversion.cpp`.
141-
This example shows how to simply convert currencies based off a given exchange rate.
142-
143-
[#examples_boost_math]
144-
== Boost.Math Integration
145-
146-
=== Bollinger Bands
147-
148-
In the examples folder there is a file named `statistics.cpp`.
149-
This example demonstrates how to parse a file, and then leverage Boost.Math to compute statistics of that data set culminating with the values of the Bollinger Bands.
150-
This example could be extended with the simple moving average to create full bands based on the period of the moving average you would like.
129+
....
130+
====
151131

152132
[#examples_format]
153133
== Formatting
@@ -158,8 +138,8 @@ pass:[{fmt}] support is available starting with pass:[C++14] so long as you have
158138
[#examples_fmt_format]
159139
=== `<fmt/format.hpp>`
160140

161-
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/fmt_format.cpp[fmt_format.cpp].
162-
141+
.This example demonstrates the various formatting options provided by the library to support usage of pass:[{fmt}]
142+
====
163143
[source, c++]
164144
----
165145
// Copyright 2025 Matt Borland
@@ -205,7 +185,7 @@ int main()
205185
----
206186
207187
Expected Output:
208-
----
188+
....
209189
Default Format:
210190
3.14
211191
3.141
@@ -221,7 +201,8 @@ Scientific Format with Specified Precision:
221201
Scientific Format with Specified Precision and Padding:
222202
03.140e+00
223203
03.141e+00
224-
----
204+
....
205+
====
225206

226207
IMPORTANT: If you are using the convenience header `<boost/decimal.hpp>` the header `<boost/decimal/fmt_format.hpp>` is *NOT* automatically included since it requires an external library.
227208
You must include it yourself.
@@ -230,15 +211,16 @@ You must include it yourself.
230211
=== `<format>`
231212

232213
Taking the above example of pass:[{fmt}] and replacing all instances of `namespace fmt` with `namespace std` gives us another working example.
233-
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/format.cpp[format.cpp]
234214

215+
.This example demonstrates how to use `<format>` with the library in-place or in addition to pass:[{fmt}]
216+
====
235217
[source, c++]
236218
----
237219
include::example$format.cpp[]
238220
----
239221
240-
Expected Output:
241-
----
222+
.Expected Output:
223+
....
242224
Default Format:
243225
3.14
244226
3.141
@@ -254,17 +236,13 @@ Scientific Format with Specified Precision:
254236
Scientific Format with Specified Precision and Padding:
255237
03.140e+00
256238
03.141e+00
257-
----
239+
....
240+
====
258241

259242

260243
[#examples_print]
261-
== `<print>`
262-
263-
We can make one final change to our `<format>` example where instead of using `std::cout`, we use pass:[C++23's] `<print>`.
264-
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/print.cpp[print.cpp].
265-
266-
267-
.`<print>` Example
244+
=== `<print>`
245+
.This example demonstrates how to use `<print>` with the library
268246
====
269247
[source, c++]
270248
----
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
////
2+
Copyright 2023 Matt Borland
3+
Distributed under the Boost Software License, Version 1.0.
4+
https://www.boost.org/LICENSE_1_0.txt
5+
////
6+
7+
[#examples_finance]
8+
= Financial Examples
9+
:idprefix: financial_examples_
10+
11+
== Financial Applications
12+
13+
=== Simple Moving Average
14+
15+
In the examples folder there is a file named `moving_average.cpp`.
16+
This example shows how to parse historical stock data from a file and use it.
17+
This serves as a framework for other calculations for securities.
18+
19+
=== Currency Conversion
20+
In the examples folder there is a file named `currency_conversion.cpp`.
21+
This example shows how to simply convert currencies based off a given exchange rate.
22+
23+
[#examples_boost_math]
24+
== Boost.Math Integration
25+
26+
=== Bollinger Bands
27+
28+
In the examples folder there is a file named `statistics.cpp`.
29+
This example demonstrates how to parse a file, and then leverage Boost.Math to compute statistics of that data set culminating with the values of the Bollinger Bands.
30+
This example could be extended with the simple moving average to create full bands based on the period of the moving average you would like.

0 commit comments

Comments
 (0)