You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+4-78Lines changed: 4 additions & 78 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,15 +10,8 @@
10
10
11
11
---
12
12
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.
22
15
23
16
# How To Use The Library
24
17
@@ -95,78 +88,11 @@ class decimal_fast128_t;
95
88
These types operate like built-in floating point types.
96
89
They have their own implementations of the Standard-Library functions
97
90
(e.g. like those found in `<cmath>`, `<charconv>`, `<cstdlib>`, etc.).
98
-
99
91
The entire library can be conveniently included with `#include <boost/decimal.hpp>`
100
92
101
-
Using the decimal types is simple.
102
-
103
-
```cpp
104
-
#include<boost/decimal.hpp>
105
-
#include<iostream>
106
-
107
-
intmain()
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
-
intmain()
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.
155
95
156
96
# Full Documentation
157
97
158
98
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
All examples can be found in the library `examples/` folder as well.
12
12
13
13
[#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
+
====
18
17
[source, c++]
19
18
----
20
19
include::example$basic_construction.cpp[]
21
20
----
22
21
23
-
The expected output from this example is:
24
-
----
22
+
.Expected Output
23
+
....
25
24
Val_1: 100
26
25
Val_2: 100
27
26
Val_3: -100
@@ -30,58 +29,60 @@ Underflow constructs zero
30
29
NaN constructs NaN
31
30
Values constructed from const char* and std::string are the same
32
31
Can not construct from invalid string
33
-
----
32
+
....
33
+
====
34
34
35
35
[#examples_promotion]
36
36
== Promotion and Mixed Decimal Arithmetic
37
37
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
+
====
40
40
[source, c++]
41
41
----
42
42
include::example$promotion.cpp[]
43
43
----
44
44
45
-
Expected Output:
46
-
----
45
+
.Expected Output:
46
+
....
47
47
decimal32_t value (a): 5.2
48
48
decimal64_t value (b): 3.9
49
49
a is greater than b
50
50
5.2 is less than 1e+385
51
51
1e+385 is now less than inf
52
52
The result of a + b is a decimal64_t: 9.1
53
-
----
53
+
....
54
+
====
54
55
55
56
[#examples_charconv]
56
57
== `<charconv>`
57
58
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
+
====
60
61
[source, c++]
61
62
----
62
63
include::example$charconv.cpp[]
63
64
----
64
65
65
-
Output:
66
-
----
66
+
.Expected Output:
67
+
....
67
68
Initial decimal: -7123450
68
69
Value from string: 3.1415
69
70
Value in scientific format: -7.12345e+06
70
71
Value in scientific format with precision 20: -7.12345000000000000000e+06
71
-
----
72
+
....
73
+
====
72
74
73
75
[#examples_generic_programming]
74
76
== 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
+
====
78
79
[source, c++]
79
80
----
80
81
include::example$adl.cpp[]
81
82
----
82
83
83
84
Expected Output:
84
-
----
85
+
....
85
86
Float:
86
87
sin(-0.5) = -0.479426
87
88
-sin(0.5) = -0.479426
@@ -105,49 +106,28 @@ sin(-0.5) = -0.479426
105
106
decimal128_t:
106
107
sin(-0.5) = -0.479426
107
108
-sin(0.5) = -0.479426
108
-
----
109
+
....
110
+
====
109
111
110
112
[#examples_literals_constants]
111
113
== 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
+
====
115
116
[source, c++]
116
117
----
117
118
include::example$literals.cpp[]
118
119
----
119
120
120
121
Expected Output:
121
-
----
122
+
....
122
123
32-bit Pi: 3.141593
123
124
64-bit Pi: 3.141592653589793
124
125
32-bit UDL Pi: 3.141593
125
126
Rounded UDL has the same value as the 32-bit constant
126
127
64-bit UDL Pi: 3.141592653589793
127
128
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
+
====
151
131
152
132
[#examples_format]
153
133
== Formatting
@@ -158,8 +138,8 @@ pass:[{fmt}] support is available starting with pass:[C++14] so long as you have
158
138
[#examples_fmt_format]
159
139
=== `<fmt/format.hpp>`
160
140
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
+
====
163
143
[source, c++]
164
144
----
165
145
// Copyright 2025 Matt Borland
@@ -205,7 +185,7 @@ int main()
205
185
----
206
186
207
187
Expected Output:
208
-
----
188
+
....
209
189
Default Format:
210
190
3.14
211
191
3.141
@@ -221,7 +201,8 @@ Scientific Format with Specified Precision:
221
201
Scientific Format with Specified Precision and Padding:
222
202
03.140e+00
223
203
03.141e+00
224
-
----
204
+
....
205
+
====
225
206
226
207
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.
227
208
You must include it yourself.
@@ -230,15 +211,16 @@ You must include it yourself.
230
211
=== `<format>`
231
212
232
213
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]
234
214
215
+
.This example demonstrates how to use `<format>` with the library in-place or in addition to pass:[{fmt}]
216
+
====
235
217
[source, c++]
236
218
----
237
219
include::example$format.cpp[]
238
220
----
239
221
240
-
Expected Output:
241
-
----
222
+
.Expected Output:
223
+
....
242
224
Default Format:
243
225
3.14
244
226
3.141
@@ -254,17 +236,13 @@ Scientific Format with Specified Precision:
254
236
Scientific Format with Specified Precision and Padding:
255
237
03.140e+00
256
238
03.141e+00
257
-
----
239
+
....
240
+
====
258
241
259
242
260
243
[#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
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