// Copyright 2025 Christopher Kormanyos // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #include #include #include #include #include #include #include namespace local { template auto test_max_convert() -> void { using float_type = FloatType; using float_array_type = std::array; const float_array_type factors = { float_type { "0.999" }, float_type { 1 }, float_type { "1.011" }, float_type { "2.022" }, float_type { "3.033" }, float_type { "4.044" }, float_type { "5.055" } }; for(std::size_t i = std::size_t { UINT8_C(0) }; i < std::tuple_size::value; ++i) { static_cast(i); using long_type = LongIshType; const float_type flt_val { float_type { (std::numeric_limits::max)() } * factors[i] }; const long_type conversion_result { static_cast(flt_val) }; if(i == std::size_t { UINT8_C(0) }) { BOOST_TEST(conversion_result < (std::numeric_limits::max)()); } else { BOOST_TEST(conversion_result == (std::numeric_limits::max)()); } } } namespace detail { template auto test_convert_neg_to_ll_types_impl(std::vector& ll_ctrls) -> void { ll_ctrls.clear(); using float_type = FloatType; using std::ldexp; using float_array_type = std::array; const float_array_type factors = { float_type { -ldexp(float_type { 1 }, -28) }, float_type { "-1.0E-9" }, float_type { "-0.999" }, float_type { -1 }, float_type { "-1.011" }, float_type { "-2.022" }, float_type { "-3.033" }, float_type { "-4.044" }, float_type { "-5.055" } }; for(std::size_t i = std::size_t { UINT8_C(0) }; i < factors.size(); ++i) { static_cast(i); using float_type = FloatType; using long_type = LongIshType; const float_type flt_val { static_cast((std::numeric_limits::max)()) * factors[i] }; const long_type conversion_result { static_cast(flt_val) }; ll_ctrls.push_back(conversion_result); } } template auto test_convert_pos_to_ll_types_impl(std::vector& ll_ctrls) -> void { ll_ctrls.clear(); using float_type = FloatType; using std::ldexp; using float_array_type = std::array; const float_array_type factors = { float_type { +ldexp(float_type { 1 }, -28) }, float_type { "+1.0E-9" }, float_type { "+0.999" }, float_type { -1 }, float_type { "+1.011" }, float_type { "+2.022" }, float_type { "+3.033" }, float_type { "+4.044" }, float_type { "+5.055" } }; for(std::size_t i = std::size_t { UINT8_C(0) }; i < factors.size(); ++i) { static_cast(i); using float_type = FloatType; using long_type = LongIshType; const float_type flt_val { static_cast((std::numeric_limits::max)()) * factors[i] }; const long_type conversion_result { static_cast(flt_val) }; ll_ctrls.push_back(conversion_result); } } } // namespace detail auto test_convert_pos_to_ll_types() -> void { std::vector ull_ctrls_dec; std::vector ull_ctrls_bin; std::vector sll_ctrls_dec; std::vector sll_ctrls_bin; detail::test_convert_pos_to_ll_types_impl<::boost::multiprecision::cpp_dec_float_50, unsigned long long>(ull_ctrls_dec); detail::test_convert_pos_to_ll_types_impl<::boost::multiprecision::cpp_bin_float_50, unsigned long long>(ull_ctrls_bin); detail::test_convert_pos_to_ll_types_impl<::boost::multiprecision::cpp_dec_float_50, signed long long>(sll_ctrls_dec); detail::test_convert_pos_to_ll_types_impl<::boost::multiprecision::cpp_bin_float_50, signed long long>(sll_ctrls_bin); BOOST_TEST(ull_ctrls_dec.size() == ull_ctrls_bin.size()); BOOST_TEST(!ull_ctrls_dec.empty()); for(std::size_t i { std::size_t { UINT8_C(0) } }; i < ull_ctrls_dec.size(); ++i) { BOOST_TEST(ull_ctrls_dec[i] == ull_ctrls_bin[i]); } BOOST_TEST(sll_ctrls_dec.size() == sll_ctrls_bin.size()); BOOST_TEST(!sll_ctrls_dec.empty()); for(std::size_t i { std::size_t { UINT8_C(0) } }; i < sll_ctrls_dec.size(); ++i) { BOOST_TEST(sll_ctrls_dec[i] == sll_ctrls_bin[i]); } } auto test_convert_neg_to_ll_types() -> void { std::vector ull_ctrls_dec; std::vector ull_ctrls_bin; std::vector sll_ctrls_dec; std::vector sll_ctrls_bin; detail::test_convert_neg_to_ll_types_impl<::boost::multiprecision::cpp_dec_float_50, unsigned long long>(ull_ctrls_dec); detail::test_convert_neg_to_ll_types_impl<::boost::multiprecision::cpp_bin_float_50, unsigned long long>(ull_ctrls_bin); detail::test_convert_neg_to_ll_types_impl<::boost::multiprecision::cpp_dec_float_50, signed long long>(sll_ctrls_dec); detail::test_convert_neg_to_ll_types_impl<::boost::multiprecision::cpp_bin_float_50, signed long long>(sll_ctrls_bin); BOOST_TEST(ull_ctrls_dec.size() == ull_ctrls_bin.size()); BOOST_TEST(!ull_ctrls_dec.empty()); for(std::size_t i { std::size_t { UINT8_C(0) } }; i < ull_ctrls_dec.size(); ++i) { BOOST_TEST(ull_ctrls_dec[i] == ull_ctrls_bin[i]); } BOOST_TEST(sll_ctrls_dec.size() == sll_ctrls_bin.size()); BOOST_TEST(!sll_ctrls_dec.empty()); for(std::size_t i { std::size_t { UINT8_C(0) } }; i < sll_ctrls_dec.size(); ++i) { BOOST_TEST(sll_ctrls_dec[i] == sll_ctrls_bin[i]); } } } // namespace local auto main() -> int { local::test_max_convert<::boost::multiprecision::cpp_dec_float_50, unsigned long long>(); local::test_max_convert<::boost::multiprecision::cpp_bin_float_50, unsigned long long>(); local::test_max_convert<::boost::multiprecision::cpp_dec_float_50, signed long long>(); local::test_max_convert<::boost::multiprecision::cpp_bin_float_50, signed long long>(); local::test_convert_neg_to_ll_types(); local::test_convert_pos_to_ll_types(); return boost::report_errors(); }