Skip to content

Commit 6bdf768

Browse files
authored
Merge pull request #745 from cppalliance/examples
Examples
2 parents a612d97 + e39e572 commit 6bdf768

File tree

16 files changed

+311
-240
lines changed

16 files changed

+311
-240
lines changed

doc/decimal/examples.adoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ int main()
9292
return 0;
9393
}
9494
----
95+
Output:
96+
----
97+
Initial Value: 0.25
98+
Returned Value: 0.25
99+
----
100+
95101

96102
== Rounding Mode
97103
[source, c++]
@@ -179,3 +185,35 @@ int main()
179185
return 0;
180186
}
181187
----
188+
189+
== Bit Conversions
190+
[source, c++]
191+
----
192+
#include <boost/decimal.hpp>
193+
#include <iostream>
194+
#include <iomanip>
195+
196+
using namespace boost::decimal;
197+
198+
int main()
199+
{
200+
const decimal32_fast fast_type {5};
201+
const std::uint32_t BID_bits {to_bid(fast_type)};
202+
const std::uint32_t DPD_bits {to_dpd(fast_type)};
203+
204+
std::cout << std::hex
205+
<< "BID format: " << BID_bits << '\n'
206+
<< "DPD format: " << DPD_bits << std::endl;
207+
208+
const decimal32 bid_decimal {from_bid<decimal32>(BID_bits)};
209+
const decimal32 dpd_decimal {from_dpd<decimal32>(DPD_bits)};
210+
211+
return !(bid_decimal == dpd_decimal);
212+
}
213+
----
214+
Output:
215+
----
216+
BID format: 31fc4b40
217+
DPD format: 35f00000
218+
----
219+

examples/bit_conversions.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2024 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 <iomanip>
8+
9+
using namespace boost::decimal;
10+
11+
int main()
12+
{
13+
const decimal32_fast fast_type {5};
14+
const std::uint32_t BID_bits {to_bid(fast_type)};
15+
const std::uint32_t DPD_bits {to_dpd(fast_type)};
16+
17+
std::cout << std::hex
18+
<< "BID format: " << BID_bits << '\n'
19+
<< "DPD format: " << DPD_bits << std::endl;
20+
21+
const decimal32 bid_decimal {from_bid<decimal32>(BID_bits)};
22+
const decimal32 dpd_decimal {from_dpd<decimal32>(DPD_bits)};
23+
24+
return !(bid_decimal == dpd_decimal);
25+
}

examples/charconv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int main()
1818
*r_to.ptr = '\0';
1919

2020
decimal64 return_value;
21-
auto r_from = from_chars(buffer, buffer + std::strlen(buffer), return_value);
21+
BOOST_DECIMAL_ATTRIBUTE_UNUSED auto r_from = from_chars(buffer, buffer + std::strlen(buffer), return_value);
2222
assert(r_from);
2323

2424
assert(val == return_value);

examples/literals.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ int main()
1717
{
1818
using namespace boost::decimal;
1919

20-
const auto pi_32 {"3.141592653589793238"_DF};
21-
const auto pi_64 {"3.141592653589793238"_DD};
20+
BOOST_DECIMAL_ATTRIBUTE_UNUSED const auto pi_32 {"3.141592653589793238"_DF};
21+
BOOST_DECIMAL_ATTRIBUTE_UNUSED const auto pi_64 {"3.141592653589793238"_DD};
2222

2323
assert(float_equal(pi_32, static_cast<decimal32>(pi_64))); // Explicit conversion between decimal types
2424
assert(float_equal(pi_32, boost::decimal::numbers::pi_v<decimal32>)); // Constants available in numbers namespace

examples/rounding_mode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
int main()
99
{
10-
auto default_rounding_mode = boost::decimal::fegetround(); // Default is fe_dec_to_nearest_from_zero
10+
BOOST_DECIMAL_ATTRIBUTE_UNUSED auto default_rounding_mode = boost::decimal::fegetround(); // Default is fe_dec_to_nearest_from_zero
1111

12-
auto new_rounding_mode = boost::decimal::fesetround(boost::decimal::rounding_mode::fe_dec_to_nearest);
12+
BOOST_DECIMAL_ATTRIBUTE_UNUSED auto new_rounding_mode = boost::decimal::fesetround(boost::decimal::rounding_mode::fe_dec_to_nearest);
1313

1414
assert(default_rounding_mode != new_rounding_mode);
1515

include/boost/decimal/charconv.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,12 +914,12 @@ BOOST_DECIMAL_EXPORT BOOST_DECIMAL_CONSTEXPR auto to_chars(char* first, char* la
914914
template <typename T>
915915
struct limits
916916
{
917-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_chars = boost::decimal::detail::max_string_length_v<T>;
917+
static constexpr int max_chars = boost::decimal::detail::max_string_length_v<T>;
918918
};
919919

920920
#if !(defined(__cpp_inline_variables) && __cpp_inline_variables >= 201606L) && (!defined(_MSC_VER) || _MSC_VER != 1900)
921921

922-
template <typename T> BOOST_DECIMAL_ATTRIBUTE_UNUSED constexpr int limits<T>::max_chars;
922+
template <typename T> constexpr int limits<T>::max_chars;
923923

924924
#endif
925925

include/boost/decimal/decimal128.hpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,45 +2187,45 @@ struct numeric_limits<boost::decimal::decimal128>
21872187
public:
21882188
#endif
21892189

2190-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_specialized = true;
2191-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_signed = true;
2192-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_integer = false;
2193-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_exact = false;
2194-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_infinity = true;
2195-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_quiet_NaN = true;
2196-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_signaling_NaN = true;
2190+
static constexpr bool is_specialized = true;
2191+
static constexpr bool is_signed = true;
2192+
static constexpr bool is_integer = false;
2193+
static constexpr bool is_exact = false;
2194+
static constexpr bool has_infinity = true;
2195+
static constexpr bool has_quiet_NaN = true;
2196+
static constexpr bool has_signaling_NaN = true;
21972197

21982198
// These members were deprecated in C++23
21992199
#if ((!defined(_MSC_VER) && (__cplusplus <= 202002L)) || (defined(_MSC_VER) && (_MSVC_LANG <= 202002L)))
2200-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr std::float_denorm_style has_denorm = std::denorm_present;
2201-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_denorm_loss = true;
2200+
static constexpr std::float_denorm_style has_denorm = std::denorm_present;
2201+
static constexpr bool has_denorm_loss = true;
22022202
#endif
22032203

2204-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr std::float_round_style round_style = std::round_indeterminate;
2205-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_iec559 = true;
2206-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_bounded = true;
2207-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_modulo = false;
2208-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int digits = 34;
2209-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int digits10 = digits;
2210-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_digits10 = digits;
2211-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int radix = 10;
2212-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int min_exponent = -6142;
2213-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int min_exponent10 = min_exponent;
2214-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_exponent = 6145;
2215-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_exponent10 = max_exponent;
2216-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool traps = numeric_limits<std::uint64_t>::traps;
2217-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool tinyness_before = true;
2204+
static constexpr std::float_round_style round_style = std::round_indeterminate;
2205+
static constexpr bool is_iec559 = true;
2206+
static constexpr bool is_bounded = true;
2207+
static constexpr bool is_modulo = false;
2208+
static constexpr int digits = 34;
2209+
static constexpr int digits10 = digits;
2210+
static constexpr int max_digits10 = digits;
2211+
static constexpr int radix = 10;
2212+
static constexpr int min_exponent = -6142;
2213+
static constexpr int min_exponent10 = min_exponent;
2214+
static constexpr int max_exponent = 6145;
2215+
static constexpr int max_exponent10 = max_exponent;
2216+
static constexpr bool traps = numeric_limits<std::uint64_t>::traps;
2217+
static constexpr bool tinyness_before = true;
22182218

22192219
// Member functions
2220-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto (min) () -> boost::decimal::decimal128 { return {1, min_exponent}; }
2221-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto (max) () -> boost::decimal::decimal128 { return {boost::decimal::detail::uint128{UINT64_C(999'999'999'999'999), UINT64_C(9'999'999'999'999'999'999)}, max_exponent}; }
2222-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto lowest () -> boost::decimal::decimal128 { return {boost::decimal::detail::uint128{UINT64_C(999'999'999'999'999), UINT64_C(9'999'999'999'999'999'999)}, max_exponent, true}; }
2223-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto epsilon () -> boost::decimal::decimal128 { return {1, -34}; }
2224-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto round_error () -> boost::decimal::decimal128 { return epsilon(); }
2225-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto infinity () -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_inf_mask); }
2226-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto quiet_NaN () -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_nan_mask); }
2227-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto signaling_NaN() -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_snan_mask); }
2228-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto denorm_min () -> boost::decimal::decimal128 { return {1, boost::decimal::detail::etiny_v<boost::decimal::decimal128>}; }
2220+
static constexpr auto (min) () -> boost::decimal::decimal128 { return {1, min_exponent}; }
2221+
static constexpr auto (max) () -> boost::decimal::decimal128 { return {boost::decimal::detail::uint128{UINT64_C(999'999'999'999'999), UINT64_C(9'999'999'999'999'999'999)}, max_exponent}; }
2222+
static constexpr auto lowest () -> boost::decimal::decimal128 { return {boost::decimal::detail::uint128{UINT64_C(999'999'999'999'999), UINT64_C(9'999'999'999'999'999'999)}, max_exponent, true}; }
2223+
static constexpr auto epsilon () -> boost::decimal::decimal128 { return {1, -34}; }
2224+
static constexpr auto round_error () -> boost::decimal::decimal128 { return epsilon(); }
2225+
static constexpr auto infinity () -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_inf_mask); }
2226+
static constexpr auto quiet_NaN () -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_nan_mask); }
2227+
static constexpr auto signaling_NaN() -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_snan_mask); }
2228+
static constexpr auto denorm_min () -> boost::decimal::decimal128 { return {1, boost::decimal::detail::etiny_v<boost::decimal::decimal128>}; }
22292229
};
22302230

22312231
} //namespace std

include/boost/decimal/decimal128_fast.hpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,45 +1428,45 @@ struct numeric_limits<boost::decimal::decimal128_fast>
14281428
public:
14291429
#endif
14301430

1431-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_specialized = true;
1432-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_signed = true;
1433-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_integer = false;
1434-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_exact = false;
1435-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_infinity = true;
1436-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_quiet_NaN = true;
1437-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_signaling_NaN = true;
1431+
static constexpr bool is_specialized = true;
1432+
static constexpr bool is_signed = true;
1433+
static constexpr bool is_integer = false;
1434+
static constexpr bool is_exact = false;
1435+
static constexpr bool has_infinity = true;
1436+
static constexpr bool has_quiet_NaN = true;
1437+
static constexpr bool has_signaling_NaN = true;
14381438

14391439
// These members were deprecated in C++23
14401440
#if ((!defined(_MSC_VER) && (__cplusplus <= 202002L)) || (defined(_MSC_VER) && (_MSVC_LANG <= 202002L)))
1441-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr std::float_denorm_style has_denorm = std::denorm_present;
1442-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_denorm_loss = true;
1441+
static constexpr std::float_denorm_style has_denorm = std::denorm_present;
1442+
static constexpr bool has_denorm_loss = true;
14431443
#endif
14441444

1445-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr std::float_round_style round_style = std::round_indeterminate;
1446-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_iec559 = false;
1447-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_bounded = true;
1448-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_modulo = false;
1449-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int digits = 34;
1450-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int digits10 = digits;
1451-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_digits10 = digits;
1452-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int radix = 10;
1453-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int min_exponent = -6142;
1454-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int min_exponent10 = min_exponent;
1455-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_exponent = 6145;
1456-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_exponent10 = max_exponent;
1457-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool traps = numeric_limits<std::uint64_t>::traps;
1458-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool tinyness_before = true;
1445+
static constexpr std::float_round_style round_style = std::round_indeterminate;
1446+
static constexpr bool is_iec559 = false;
1447+
static constexpr bool is_bounded = true;
1448+
static constexpr bool is_modulo = false;
1449+
static constexpr int digits = 34;
1450+
static constexpr int digits10 = digits;
1451+
static constexpr int max_digits10 = digits;
1452+
static constexpr int radix = 10;
1453+
static constexpr int min_exponent = -6142;
1454+
static constexpr int min_exponent10 = min_exponent;
1455+
static constexpr int max_exponent = 6145;
1456+
static constexpr int max_exponent10 = max_exponent;
1457+
static constexpr bool traps = numeric_limits<std::uint64_t>::traps;
1458+
static constexpr bool tinyness_before = true;
14591459

14601460
// Member functions
1461-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto (min) () -> boost::decimal::decimal128_fast { return {1, min_exponent}; }
1462-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto (max) () -> boost::decimal::decimal128_fast { return {boost::decimal::detail::uint128{UINT64_C(999'999'999'999'999), UINT64_C(9'999'999'999'999'999'999)}, max_exponent}; }
1463-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto lowest () -> boost::decimal::decimal128_fast { return {boost::decimal::detail::uint128{UINT64_C(999'999'999'999'999), UINT64_C(9'999'999'999'999'999'999)}, max_exponent, true}; }
1464-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto epsilon () -> boost::decimal::decimal128_fast { return {1, -34}; }
1465-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto round_error () -> boost::decimal::decimal128_fast { return epsilon(); }
1466-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto infinity () -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_inf, 0, false); }
1467-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto quiet_NaN () -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_qnan, 0, false); }
1468-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto signaling_NaN() -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_snan, 0, false); }
1469-
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto denorm_min () -> boost::decimal::decimal128_fast { return {1, boost::decimal::detail::etiny_v<boost::decimal::decimal128>}; }
1461+
static constexpr auto (min) () -> boost::decimal::decimal128_fast { return {1, min_exponent}; }
1462+
static constexpr auto (max) () -> boost::decimal::decimal128_fast { return {boost::decimal::detail::uint128{UINT64_C(999'999'999'999'999), UINT64_C(9'999'999'999'999'999'999)}, max_exponent}; }
1463+
static constexpr auto lowest () -> boost::decimal::decimal128_fast { return {boost::decimal::detail::uint128{UINT64_C(999'999'999'999'999), UINT64_C(9'999'999'999'999'999'999)}, max_exponent, true}; }
1464+
static constexpr auto epsilon () -> boost::decimal::decimal128_fast { return {1, -34}; }
1465+
static constexpr auto round_error () -> boost::decimal::decimal128_fast { return epsilon(); }
1466+
static constexpr auto infinity () -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_inf, 0, false); }
1467+
static constexpr auto quiet_NaN () -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_qnan, 0, false); }
1468+
static constexpr auto signaling_NaN() -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_snan, 0, false); }
1469+
static constexpr auto denorm_min () -> boost::decimal::decimal128_fast { return {1, boost::decimal::detail::etiny_v<boost::decimal::decimal128>}; }
14701470
};
14711471

14721472
}

0 commit comments

Comments
 (0)