Skip to content

Commit

Permalink
Merge pull request #745 from cppalliance/examples
Browse files Browse the repository at this point in the history
Examples
  • Loading branch information
mborland authored Oct 3, 2024
2 parents a612d97 + e39e572 commit 6bdf768
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 240 deletions.
38 changes: 38 additions & 0 deletions doc/decimal/examples.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ int main()
return 0;
}
----
Output:
----
Initial Value: 0.25
Returned Value: 0.25
----


== Rounding Mode
[source, c++]
Expand Down Expand Up @@ -179,3 +185,35 @@ int main()
return 0;
}
----

== Bit Conversions
[source, c++]
----
#include <boost/decimal.hpp>
#include <iostream>
#include <iomanip>
using namespace boost::decimal;
int main()
{
const decimal32_fast fast_type {5};
const std::uint32_t BID_bits {to_bid(fast_type)};
const std::uint32_t DPD_bits {to_dpd(fast_type)};
std::cout << std::hex
<< "BID format: " << BID_bits << '\n'
<< "DPD format: " << DPD_bits << std::endl;
const decimal32 bid_decimal {from_bid<decimal32>(BID_bits)};
const decimal32 dpd_decimal {from_dpd<decimal32>(DPD_bits)};
return !(bid_decimal == dpd_decimal);
}
----
Output:
----
BID format: 31fc4b40
DPD format: 35f00000
----

25 changes: 25 additions & 0 deletions examples/bit_conversions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/decimal.hpp>
#include <iostream>
#include <iomanip>

using namespace boost::decimal;

int main()
{
const decimal32_fast fast_type {5};
const std::uint32_t BID_bits {to_bid(fast_type)};
const std::uint32_t DPD_bits {to_dpd(fast_type)};

std::cout << std::hex
<< "BID format: " << BID_bits << '\n'
<< "DPD format: " << DPD_bits << std::endl;

const decimal32 bid_decimal {from_bid<decimal32>(BID_bits)};
const decimal32 dpd_decimal {from_dpd<decimal32>(DPD_bits)};

return !(bid_decimal == dpd_decimal);
}
2 changes: 1 addition & 1 deletion examples/charconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main()
*r_to.ptr = '\0';

decimal64 return_value;
auto r_from = from_chars(buffer, buffer + std::strlen(buffer), return_value);
BOOST_DECIMAL_ATTRIBUTE_UNUSED auto r_from = from_chars(buffer, buffer + std::strlen(buffer), return_value);
assert(r_from);

assert(val == return_value);
Expand Down
4 changes: 2 additions & 2 deletions examples/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ int main()
{
using namespace boost::decimal;

const auto pi_32 {"3.141592653589793238"_DF};
const auto pi_64 {"3.141592653589793238"_DD};
BOOST_DECIMAL_ATTRIBUTE_UNUSED const auto pi_32 {"3.141592653589793238"_DF};
BOOST_DECIMAL_ATTRIBUTE_UNUSED const auto pi_64 {"3.141592653589793238"_DD};

assert(float_equal(pi_32, static_cast<decimal32>(pi_64))); // Explicit conversion between decimal types
assert(float_equal(pi_32, boost::decimal::numbers::pi_v<decimal32>)); // Constants available in numbers namespace
Expand Down
4 changes: 2 additions & 2 deletions examples/rounding_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

int main()
{
auto default_rounding_mode = boost::decimal::fegetround(); // Default is fe_dec_to_nearest_from_zero
BOOST_DECIMAL_ATTRIBUTE_UNUSED auto default_rounding_mode = boost::decimal::fegetround(); // Default is fe_dec_to_nearest_from_zero

auto new_rounding_mode = boost::decimal::fesetround(boost::decimal::rounding_mode::fe_dec_to_nearest);
BOOST_DECIMAL_ATTRIBUTE_UNUSED auto new_rounding_mode = boost::decimal::fesetround(boost::decimal::rounding_mode::fe_dec_to_nearest);

assert(default_rounding_mode != new_rounding_mode);

Expand Down
4 changes: 2 additions & 2 deletions include/boost/decimal/charconv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,12 +914,12 @@ BOOST_DECIMAL_EXPORT BOOST_DECIMAL_CONSTEXPR auto to_chars(char* first, char* la
template <typename T>
struct limits
{
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_chars = boost::decimal::detail::max_string_length_v<T>;
static constexpr int max_chars = boost::decimal::detail::max_string_length_v<T>;
};

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

template <typename T> BOOST_DECIMAL_ATTRIBUTE_UNUSED constexpr int limits<T>::max_chars;
template <typename T> constexpr int limits<T>::max_chars;

#endif

Expand Down
64 changes: 32 additions & 32 deletions include/boost/decimal/decimal128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2187,45 +2187,45 @@ struct numeric_limits<boost::decimal::decimal128>
public:
#endif

BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_specialized = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_signed = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_integer = false;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_exact = false;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_infinity = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_quiet_NaN = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_signaling_NaN = true;
static constexpr bool is_specialized = true;
static constexpr bool is_signed = true;
static constexpr bool is_integer = false;
static constexpr bool is_exact = false;
static constexpr bool has_infinity = true;
static constexpr bool has_quiet_NaN = true;
static constexpr bool has_signaling_NaN = true;

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

BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr std::float_round_style round_style = std::round_indeterminate;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_iec559 = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_bounded = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_modulo = false;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int digits = 34;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int digits10 = digits;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_digits10 = digits;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int radix = 10;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int min_exponent = -6142;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int min_exponent10 = min_exponent;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_exponent = 6145;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_exponent10 = max_exponent;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool traps = numeric_limits<std::uint64_t>::traps;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool tinyness_before = true;
static constexpr std::float_round_style round_style = std::round_indeterminate;
static constexpr bool is_iec559 = true;
static constexpr bool is_bounded = true;
static constexpr bool is_modulo = false;
static constexpr int digits = 34;
static constexpr int digits10 = digits;
static constexpr int max_digits10 = digits;
static constexpr int radix = 10;
static constexpr int min_exponent = -6142;
static constexpr int min_exponent10 = min_exponent;
static constexpr int max_exponent = 6145;
static constexpr int max_exponent10 = max_exponent;
static constexpr bool traps = numeric_limits<std::uint64_t>::traps;
static constexpr bool tinyness_before = true;

// Member functions
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto (min) () -> boost::decimal::decimal128 { return {1, min_exponent}; }
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}; }
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}; }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto epsilon () -> boost::decimal::decimal128 { return {1, -34}; }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto round_error () -> boost::decimal::decimal128 { return epsilon(); }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto infinity () -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_inf_mask); }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto quiet_NaN () -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_nan_mask); }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto signaling_NaN() -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_snan_mask); }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto denorm_min () -> boost::decimal::decimal128 { return {1, boost::decimal::detail::etiny_v<boost::decimal::decimal128>}; }
static constexpr auto (min) () -> boost::decimal::decimal128 { return {1, min_exponent}; }
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}; }
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}; }
static constexpr auto epsilon () -> boost::decimal::decimal128 { return {1, -34}; }
static constexpr auto round_error () -> boost::decimal::decimal128 { return epsilon(); }
static constexpr auto infinity () -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_inf_mask); }
static constexpr auto quiet_NaN () -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_nan_mask); }
static constexpr auto signaling_NaN() -> boost::decimal::decimal128 { return boost::decimal::from_bits(boost::decimal::detail::d128_snan_mask); }
static constexpr auto denorm_min () -> boost::decimal::decimal128 { return {1, boost::decimal::detail::etiny_v<boost::decimal::decimal128>}; }
};

} //namespace std
Expand Down
64 changes: 32 additions & 32 deletions include/boost/decimal/decimal128_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1428,45 +1428,45 @@ struct numeric_limits<boost::decimal::decimal128_fast>
public:
#endif

BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_specialized = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_signed = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_integer = false;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_exact = false;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_infinity = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_quiet_NaN = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool has_signaling_NaN = true;
static constexpr bool is_specialized = true;
static constexpr bool is_signed = true;
static constexpr bool is_integer = false;
static constexpr bool is_exact = false;
static constexpr bool has_infinity = true;
static constexpr bool has_quiet_NaN = true;
static constexpr bool has_signaling_NaN = true;

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

BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr std::float_round_style round_style = std::round_indeterminate;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_iec559 = false;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_bounded = true;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool is_modulo = false;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int digits = 34;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int digits10 = digits;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_digits10 = digits;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int radix = 10;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int min_exponent = -6142;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int min_exponent10 = min_exponent;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_exponent = 6145;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr int max_exponent10 = max_exponent;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool traps = numeric_limits<std::uint64_t>::traps;
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr bool tinyness_before = true;
static constexpr std::float_round_style round_style = std::round_indeterminate;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = true;
static constexpr bool is_modulo = false;
static constexpr int digits = 34;
static constexpr int digits10 = digits;
static constexpr int max_digits10 = digits;
static constexpr int radix = 10;
static constexpr int min_exponent = -6142;
static constexpr int min_exponent10 = min_exponent;
static constexpr int max_exponent = 6145;
static constexpr int max_exponent10 = max_exponent;
static constexpr bool traps = numeric_limits<std::uint64_t>::traps;
static constexpr bool tinyness_before = true;

// Member functions
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto (min) () -> boost::decimal::decimal128_fast { return {1, min_exponent}; }
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}; }
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}; }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto epsilon () -> boost::decimal::decimal128_fast { return {1, -34}; }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto round_error () -> boost::decimal::decimal128_fast { return epsilon(); }
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); }
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); }
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); }
BOOST_DECIMAL_ATTRIBUTE_UNUSED static constexpr auto denorm_min () -> boost::decimal::decimal128_fast { return {1, boost::decimal::detail::etiny_v<boost::decimal::decimal128>}; }
static constexpr auto (min) () -> boost::decimal::decimal128_fast { return {1, min_exponent}; }
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}; }
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}; }
static constexpr auto epsilon () -> boost::decimal::decimal128_fast { return {1, -34}; }
static constexpr auto round_error () -> boost::decimal::decimal128_fast { return epsilon(); }
static constexpr auto infinity () -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_inf, 0, false); }
static constexpr auto quiet_NaN () -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_qnan, 0, false); }
static constexpr auto signaling_NaN() -> boost::decimal::decimal128_fast { return boost::decimal::direct_init_d128(boost::decimal::detail::d128_fast_snan, 0, false); }
static constexpr auto denorm_min () -> boost::decimal::decimal128_fast { return {1, boost::decimal::detail::etiny_v<boost::decimal::decimal128>}; }
};

}
Expand Down
Loading

0 comments on commit 6bdf768

Please sign in to comment.