Skip to content

Commit 506d95c

Browse files
committed
Add example
1 parent 5c6a236 commit 506d95c

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2025 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
5+
#include <boost/decimal.hpp>
6+
#include <iostream>
7+
#include <array>
8+
#include <cstring>
9+
#include <string>
10+
11+
static constexpr std::size_t N {7};
12+
13+
// All the following decimal values will compare equal,
14+
// but since they have different numbers of 0s in the significand they will not be bitwise equal
15+
constexpr std::array<boost::decimal::decimal32_t, N> decimals = {
16+
boost::decimal::decimal32_t{3, 2},
17+
boost::decimal::decimal32_t{30, 1},
18+
boost::decimal::decimal32_t{300, 0},
19+
boost::decimal::decimal32_t{3000, -1},
20+
boost::decimal::decimal32_t{30000, -2},
21+
boost::decimal::decimal32_t{300000, -3},
22+
boost::decimal::decimal32_t{3000000, -4},
23+
};
24+
25+
// These strings represent the same values as the constructed ones shown above
26+
constexpr std::array<const char*, N> strings = {
27+
"3e+02",
28+
"3.0e+02",
29+
"3.00e+02",
30+
"3.000e+02",
31+
"3.0000e+02",
32+
"3.00000e+02",
33+
"3.000000e+02",
34+
};
35+
36+
int main()
37+
{
38+
using namespace boost::decimal;
39+
40+
// In some instances we want to preserve the cohort of our values
41+
// In the above strings array all of these values compare equal,
42+
// but will NOT be bitwise equal once constructed.
43+
44+
for (std::size_t i = 0; i < N; ++i)
45+
{
46+
decimal32_t string_val;
47+
const auto r_from = from_chars(strings[i], string_val, chars_format::cohort_preserving_scientific);
48+
49+
if (!r_from)
50+
{
51+
// Unexpected failure
52+
return 1;
53+
}
54+
55+
for (std::size_t j = 0; j < N; ++j)
56+
{
57+
// Now that we have constructed a value from string
58+
// we can compare it bitwise to all the members of the decimal array
59+
// to show the difference between operator== and bitwise equality
60+
//
61+
// All members of a cohort are supposed to compare equal with operator==,
62+
// and likewise will hash equal to
63+
std::uint32_t string_val_bits;
64+
std::uint32_t constructed_val_bits;
65+
66+
std::memcpy(&string_val_bits, &string_val, sizeof(string_val_bits));
67+
std::memcpy(&constructed_val_bits, &decimals[j], sizeof(constructed_val_bits));
68+
69+
if (string_val == decimals[j])
70+
{
71+
std::cout << "Values are equal and ";
72+
if (string_val_bits == constructed_val_bits)
73+
{
74+
std::cout << "bitwise equal.\n";
75+
}
76+
else
77+
{
78+
std::cout << "NOT bitwise equal.\n";
79+
}
80+
}
81+
}
82+
83+
// The same chars_format option applies to to_chars which allows us to roundtrip the values
84+
char buffer[64] {};
85+
const auto r_to = to_chars(buffer, buffer + sizeof(buffer), string_val, chars_format::cohort_preserving_scientific);
86+
87+
if (!r_to)
88+
{
89+
// Unexpected failure
90+
return 1;
91+
}
92+
93+
*r_to.ptr = '\0'; // charconv does not null terminate per the C++ specification
94+
95+
if (std::strcmp(strings[i], buffer) == 0)
96+
{
97+
std::cout << "Successful Roundtrip\n\n";
98+
}
99+
else
100+
{
101+
std::cout << "Failed\n\n";
102+
return 1;
103+
}
104+
}
105+
106+
return 0;
107+
}

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ run ../examples/adl.cpp ;
190190
run ../examples/basic_construction.cpp ;
191191
run ../examples/bit_conversions.cpp ;
192192
run ../examples/charconv.cpp ;
193+
run ../examples/charconv_cohort_preservation.cpp ;
193194
run ../examples/literals.cpp ;
194195
run ../examples/rounding_mode.cpp ;
195196
run ../examples/rounding_mode_compile_time.cpp ;

0 commit comments

Comments
 (0)