Skip to content

Commit 72e73ab

Browse files
committed
Add first complete and working example
1 parent d3bb69b commit 72e73ab

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

doc/modules/ROOT/pages/basics.adoc

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,19 @@ For more information see: xref:cfenv.adoc[]
119119
The entire library can be accessed using the convenience header `<boost/decimal.hpp>`.
120120
A short example of the basic usage:
121121

122+
.This https://github.com/cppalliance/decimal/blob/develop/examples/first_example.cpp[example] shows the very basics on how to make a complete and working program using the decimal library
123+
====
122124
[source, c++]
123125
----
124-
#include <boost/decimal.hpp>
125-
#include <iostream>
126-
#include <iomanip>
127-
128-
int main()
129-
{
130-
using namespace boost::decimal;
131-
132-
// Outputs 0.30000000000000004
133-
std::cout << std::setprecision(17) << 0.1 + 0.2 << std::endl;
134-
135-
// Construct the two decimal values
136-
constexpr decimal64_t a {1, -1}; // 1e-1 or 0.1
137-
constexpr decimal64_t b {2, -1}; // 2e-1 or 0.2
126+
include::example$first_example.cpp[]
127+
----
138128
139-
// Outputs 0.30000000000000000
140-
std::cout << a + b << std::endl;
129+
.Expected Output
130+
....
131+
Using doubles:
132+
0.1 + 0.2 = 0.30000000000000004
141133
142-
return 0;
143-
}
144-
145-
----
134+
Using decimal64_t:
135+
0.1_DD + 0.2_DD = 0.3000000000000000
136+
....
137+
====

examples/first_example.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ int main()
1010
{
1111
using namespace boost::decimal::literals; // The literals are in their own namespace like std::literals
1212

13-
// First we
14-
std::cout << std::setprecision(17)
13+
// First we show the result of 0.1 + 0.2 using regular doubles
14+
std::cout << std::fixed << std::setprecision(17)
15+
<< "Using doubles:\n"
1516
<< "0.1 + 0.2 = " << 0.1 + 0.2 << "\n\n";
1617

1718
// Construct the two decimal values
1819
// We construct using the literals defined by the library
1920
constexpr boost::decimal::decimal64_t a {0.1_DD};
2021
constexpr boost::decimal::decimal64_t b {0.2_DD};
2122

22-
std::cout << "Using decimals: "
23+
// Now we display the result of the same calculation using decimal64_t
24+
std::cout << "Using decimal64_t:\n"
2325
<< "0.1_DD + 0.2_DD = " << a + b << std::endl;
2426
}

0 commit comments

Comments
 (0)