Skip to content

Commit 04a51a5

Browse files
committed
Update cohort preservation charconv example
Closes: #1210
1 parent a57468d commit 04a51a5

File tree

2 files changed

+72
-112
lines changed

2 files changed

+72
-112
lines changed

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
----

examples/charconv_cohort_preservation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ constexpr std::array<const char*, N> strings = {
3939

4040
int main()
4141
{
42-
using boost::decimal::decimal32_t;
43-
using boost::decimal::from_chars;
44-
using boost::decimal::chars_format;
42+
using boost::decimal::decimal32_t; // For type decimal32_t
43+
using boost::decimal::from_chars; // decimal specific from_chars
44+
using boost::decimal::chars_format; // chars_format enum with decimal specific option shown here
4545

4646
// In some instances we want to preserve the cohort of our values
4747
// In the above strings array all of these values compare equal,
@@ -100,7 +100,7 @@ int main()
100100

101101
if (std::strcmp(strings[i], buffer) == 0)
102102
{
103-
std::cout << "Successful Roundtrip\n\n";
103+
std::cout << "Successful Roundtrip of value: " << buffer << "\n\n";
104104
}
105105
else
106106
{

0 commit comments

Comments
 (0)