Skip to content

Commit ea46b32

Browse files
authored
Merge pull request #1232 from cppalliance/more_examples
More rewrites of example
2 parents 60514f2 + c505128 commit ea46b32

File tree

11 files changed

+301
-540
lines changed

11 files changed

+301
-540
lines changed

.github/workflows/ci.yml

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -987,52 +987,53 @@ jobs:
987987
g++ main.cpp $(pkg-config --cflags --libs boost_decimal) -o test_pkgconfig
988988
./test_pkgconfig
989989

990-
MSYS2:
991-
defaults:
992-
run:
993-
shell: msys2 {0}
994-
strategy:
995-
fail-fast: false
996-
matrix:
997-
include:
998-
- { sys: MINGW32, compiler: gcc, cxxstd: '03,11,17,20' }
999-
- { sys: MINGW64, compiler: gcc, cxxstd: '03,11,17,20' }
1000-
1001-
runs-on: windows-latest
1002-
1003-
steps:
1004-
- uses: actions/checkout@v4
1005-
1006-
- name: Setup MSYS2 environment
1007-
uses: msys2/setup-msys2@v2
1008-
with:
1009-
msystem: ${{matrix.sys}}
1010-
update: true
1011-
install: git python
1012-
pacboy: gcc:p cmake:p ninja:p
1013-
1014-
- name: Fetch Boost.CI
1015-
uses: actions/checkout@v4
1016-
with:
1017-
repository: boostorg/boost-ci
1018-
ref: master
1019-
path: boost-ci-cloned
1020-
- name: Get CI scripts folder
1021-
run: |
1022-
# Copy ci folder if not testing Boost.CI
1023-
[[ "$GITHUB_REPOSITORY" =~ "boost-ci" ]] || cp -r boost-ci-cloned/ci .
1024-
rm -rf boost-ci-cloned
1025-
1026-
- name: Setup Boost
1027-
env:
1028-
B2_COMPILER: ${{matrix.compiler}}
1029-
B2_CXXSTD: ${{matrix.cxxstd}}
1030-
B2_SANITIZE: ${{matrix.sanitize}}
1031-
B2_STDLIB: ${{matrix.stdlib}}
1032-
run: ci/github/install.sh
1033-
1034-
- name: Run tests
1035-
run: ci/build.sh
990+
# Symlink in docs folder breaks these runs
991+
# MSYS2:
992+
# defaults:
993+
# run:
994+
# shell: msys2 {0}
995+
# strategy:
996+
# fail-fast: false
997+
# matrix:
998+
# include:
999+
# - { sys: MINGW32, compiler: gcc, cxxstd: '03,11,17,20' }
1000+
# - { sys: MINGW64, compiler: gcc, cxxstd: '03,11,17,20' }
1001+
#
1002+
# runs-on: windows-latest
1003+
#
1004+
# steps:
1005+
# - uses: actions/checkout@v4
1006+
#
1007+
# - name: Setup MSYS2 environment
1008+
# uses: msys2/setup-msys2@v2
1009+
# with:
1010+
# msystem: ${{matrix.sys}}
1011+
# update: true
1012+
# install: git python
1013+
# pacboy: gcc:p cmake:p ninja:p
1014+
#
1015+
# - name: Fetch Boost.CI
1016+
# uses: actions/checkout@v4
1017+
# with:
1018+
# repository: boostorg/boost-ci
1019+
# ref: master
1020+
# path: boost-ci-cloned
1021+
# - name: Get CI scripts folder
1022+
# run: |
1023+
# # Copy ci folder if not testing Boost.CI
1024+
# [[ "$GITHUB_REPOSITORY" =~ "boost-ci" ]] || cp -r boost-ci-cloned/ci .
1025+
# rm -rf boost-ci-cloned
1026+
#
1027+
# - name: Setup Boost
1028+
# env:
1029+
# B2_COMPILER: ${{matrix.compiler}}
1030+
# B2_CXXSTD: ${{matrix.cxxstd}}
1031+
# B2_SANITIZE: ${{matrix.sanitize}}
1032+
# B2_STDLIB: ${{matrix.stdlib}}
1033+
# run: ci/github/install.sh
1034+
#
1035+
# - name: Run tests
1036+
# run: ci/build.sh
10361037

10371038
intel:
10381039
runs-on: ubuntu-latest

doc/build_antora.sh

100644100755
File mode changed.

doc/modules/ROOT/examples

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../examples

doc/modules/ROOT/nav.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
** xref:examples.adoc#examples_finance[Financial Applications]
1010
** xref:examples.adoc#examples_boost_math[Boost.Math Integration]
1111
** xref:examples.adoc#examples_format[Formatting]
12+
*** xref:examples.adoc#examples_fmt_format[pass:[{fmt}]]
1213
*** xref:examples.adoc#examples_std_format[`<format>`]
13-
*** xref:examples.adoc#examples_fmt_format[+{fmt}+]
1414
** xref:examples.adoc#examples_print[`<print>`]
1515
* xref:api_reference.adoc[]
1616
** xref:api_reference.adoc#api_ref_types[Types]

doc/modules/ROOT/pages/cohorts.adoc

Lines changed: 68 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -29,115 +29,75 @@ Below is an example of how cohorts can be preserved if one so wishes.
2929

3030
== Cohort Preserving `<charconv>` Example
3131

32+
This example can be found in the `examples/` folder as https://github.com/cppalliance/decimal/blob/develop/examples/charconv_cohort_preservation.cpp[charconv_cohort_preservation.cpp].
33+
3234
[source, c++]
3335
----
34-
// This file demonstrates the effects of cohorts and how to maintain them with <charconv>
35-
36-
#include <boost/decimal/decimal32_t.hpp> // For the type decimal32_t
37-
#include <boost/decimal/charconv.hpp> // For decimal support for <charconv>
38-
#include <boost/decimal/iostream.hpp> // For decimal support for <iostream>
39-
#include <iostream>
40-
#include <array>
41-
#include <cstring>
42-
#include <string>
43-
44-
static constexpr std::size_t N {7};
45-
46-
// All the following decimal values will compare equal,
47-
// but since they have different numbers of 0s in the significand they will not be bitwise equal
48-
constexpr std::array<boost::decimal::decimal32_t, N> decimals = {
49-
boost::decimal::decimal32_t{3, 2},
50-
boost::decimal::decimal32_t{30, 1},
51-
boost::decimal::decimal32_t{300, 0},
52-
boost::decimal::decimal32_t{3000, -1},
53-
boost::decimal::decimal32_t{30000, -2},
54-
boost::decimal::decimal32_t{300000, -3},
55-
boost::decimal::decimal32_t{3000000, -4},
56-
};
57-
58-
// These strings represent the same values as the constructed ones shown above
59-
constexpr std::array<const char*, N> strings = {
60-
"3e+02",
61-
"3.0e+02",
62-
"3.00e+02",
63-
"3.000e+02",
64-
"3.0000e+02",
65-
"3.00000e+02",
66-
"3.000000e+02",
67-
};
68-
69-
int main()
70-
{
71-
using boost::decimal::decimal32_t;
72-
using boost::decimal::from_chars;
73-
using boost::decimal::chars_format;
74-
75-
// In some instances we want to preserve the cohort of our values
76-
// In the above strings array all of these values compare equal,
77-
// but will NOT be bitwise equal once constructed.
78-
79-
for (std::size_t i = 0; i < N; ++i)
80-
{
81-
decimal32_t string_val;
82-
const auto r_from = from_chars(strings[i], string_val, chars_format::cohort_preserving_scientific);
83-
84-
if (!r_from)
85-
{
86-
// Unexpected failure
87-
return 1;
88-
}
89-
90-
for (std::size_t j = 0; j < N; ++j)
91-
{
92-
// Now that we have constructed a value from string
93-
// we can compare it bitwise to all the members of the decimal array
94-
// to show the difference between operator== and bitwise equality
95-
//
96-
// All members of a cohort are supposed to compare equal with operator==,
97-
// and likewise will hash equal to
98-
std::uint32_t string_val_bits;
99-
std::uint32_t constructed_val_bits;
100-
101-
std::memcpy(&string_val_bits, &string_val, sizeof(string_val_bits));
102-
std::memcpy(&constructed_val_bits, &decimals[j], sizeof(constructed_val_bits));
103-
104-
if (string_val == decimals[j])
105-
{
106-
std::cout << "Values are equal and ";
107-
if (string_val_bits == constructed_val_bits)
108-
{
109-
std::cout << "bitwise equal.\n";
110-
}
111-
else
112-
{
113-
std::cout << "NOT bitwise equal.\n";
114-
}
115-
}
116-
}
117-
118-
// The same chars_format option applies to to_chars which allows us to roundtrip the values
119-
char buffer[64] {};
120-
const auto r_to = to_chars(buffer, buffer + sizeof(buffer), string_val, chars_format::cohort_preserving_scientific);
121-
122-
if (!r_to)
123-
{
124-
// Unexpected failure
125-
return 1;
126-
}
127-
128-
*r_to.ptr = '\0'; // charconv does not null terminate per the C++ specification
129-
130-
if (std::strcmp(strings[i], buffer) == 0)
131-
{
132-
std::cout << "Successful Roundtrip\n\n";
133-
}
134-
else
135-
{
136-
std::cout << "Failed\n\n";
137-
return 1;
138-
}
139-
}
36+
include::example$charconv_cohort_preservation.cpp[]
37+
----
14038

141-
return 0;
142-
}
39+
Expected Output:
40+
----
41+
Values are equal and bitwise equal.
42+
Values are equal and NOT bitwise equal.
43+
Values are equal and NOT bitwise equal.
44+
Values are equal and NOT bitwise equal.
45+
Values are equal and NOT bitwise equal.
46+
Values are equal and NOT bitwise equal.
47+
Values are equal and NOT bitwise equal.
48+
Successful Roundtrip of value: 3e+02
49+
50+
Values are equal and NOT bitwise equal.
51+
Values are equal and bitwise equal.
52+
Values are equal and NOT bitwise equal.
53+
Values are equal and NOT bitwise equal.
54+
Values are equal and NOT bitwise equal.
55+
Values are equal and NOT bitwise equal.
56+
Values are equal and NOT bitwise equal.
57+
Successful Roundtrip of value: 3.0e+02
58+
59+
Values are equal and NOT bitwise equal.
60+
Values are equal and NOT bitwise equal.
61+
Values are equal and bitwise equal.
62+
Values are equal and NOT bitwise equal.
63+
Values are equal and NOT bitwise equal.
64+
Values are equal and NOT bitwise equal.
65+
Values are equal and NOT bitwise equal.
66+
Successful Roundtrip of value: 3.00e+02
67+
68+
Values are equal and NOT bitwise equal.
69+
Values are equal and NOT bitwise equal.
70+
Values are equal and NOT bitwise equal.
71+
Values are equal and bitwise equal.
72+
Values are equal and NOT bitwise equal.
73+
Values are equal and NOT bitwise equal.
74+
Values are equal and NOT bitwise equal.
75+
Successful Roundtrip of value: 3.000e+02
76+
77+
Values are equal and NOT bitwise equal.
78+
Values are equal and NOT bitwise equal.
79+
Values are equal and NOT bitwise equal.
80+
Values are equal and NOT bitwise equal.
81+
Values are equal and bitwise equal.
82+
Values are equal and NOT bitwise equal.
83+
Values are equal and NOT bitwise equal.
84+
Successful Roundtrip of value: 3.0000e+02
85+
86+
Values are equal and NOT bitwise equal.
87+
Values are equal and NOT bitwise equal.
88+
Values are equal and NOT bitwise equal.
89+
Values are equal and NOT bitwise equal.
90+
Values are equal and NOT bitwise equal.
91+
Values are equal and bitwise equal.
92+
Values are equal and NOT bitwise equal.
93+
Successful Roundtrip of value: 3.00000e+02
94+
95+
Values are equal and NOT bitwise equal.
96+
Values are equal and NOT bitwise equal.
97+
Values are equal and NOT bitwise equal.
98+
Values are equal and NOT bitwise equal.
99+
Values are equal and NOT bitwise equal.
100+
Values are equal and NOT bitwise equal.
101+
Values are equal and bitwise equal.
102+
Successful Roundtrip of value: 3.000000e+02
143103
----

0 commit comments

Comments
 (0)