/////////////////////////////////////////////////////////////// // Copyright 2011 - 2025 John Maddock. // Copyright Christopher Kormanyos 2021 - 2025. // Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt // // This work is based on an earlier work: // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations", // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469 #ifdef _MSC_VER #define _SCL_SECURE_NO_WARNINGS #endif #include #include #include #if !defined(TEST_MPF_50) && !defined(TEST_MPF) && !defined(TEST_BACKEND) && !defined(TEST_CPP_DEC_FLOAT) && !defined(TEST_MPFR) && !defined(TEST_MPFR_50) && !defined(TEST_MPFI_50) && !defined(TEST_FLOAT128) && !defined(TEST_CPP_BIN_FLOAT) && !defined(TEST_CPP_DOUBLE_FLOAT) #define TEST_MPF_50 //# define TEST_MPF #define TEST_BACKEND #define TEST_CPP_DEC_FLOAT #define TEST_MPFR_50 #define TEST_MPFI_50 #define TEST_CPP_BIN_FLOAT #define TEST_CPP_DOUBLE_FLOAT #ifdef _MSC_VER #pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!") #endif #ifdef __GNUC__ #pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!" #endif #endif #include // Note: include this AFTER the test-backends are defined #include #include #if defined(TEST_MPF_50) #include #endif #if defined(TEST_MPFR_50) #include #endif #if defined(TEST_MPFI_50) #include #endif #ifdef TEST_BACKEND #include #endif #ifdef TEST_CPP_DEC_FLOAT #include #endif #ifdef TEST_FLOAT128 #include #endif #ifdef TEST_CPP_BIN_FLOAT #include #endif #ifdef TEST_CPP_DOUBLE_FLOAT #include #endif namespace local { template struct data_maker { static auto get_data() -> const DataArrayType& { static const DataArrayType instance { }; return instance; } }; template <> struct data_maker final { using local_array_type = test_pow_data::test_pow_data_array_type_reduced; static auto get_data() -> const local_array_type& { static const local_array_type instance { test_pow_data::data_reduced }; return instance; } }; template <> struct data_maker final { using local_array_type = test_pow_data::test_pow_data_array_type_default; static auto get_data() -> const local_array_type& { static const local_array_type instance { test_pow_data::data }; return instance; } }; } // namespace local template void test() { std::cout << "Testing type: " << typeid(T).name() << std::endl; using local_test_pow_data_array_type = typename std::conditional<::has_poor_exp_range_or_precision_support::value, test_pow_data::test_pow_data_array_type_reduced, test_pow_data::test_pow_data_array_type_default>::type; const auto& data = local::data_maker::get_data(); unsigned max_err = 0; for (unsigned k = 0; k < data.size(); k++) { T val = pow(T(data[k][0]), T(data[k][1])); T e = relative_error(val, T(data[k][2])); unsigned err = e.template convert_to(); if (err > max_err) { max_err = err; } val = pow(T(data[k][0]), -T(data[k][1])); e = relative_error(val, T(1 / T(data[k][2]))); err = e.template convert_to(); if (err > max_err) { max_err = err; } } std::cout << "Max error was: " << max_err << std::endl; // // Special cases come last: // BOOST_CHECK_EQUAL(pow(T(0), T(0)), 1); BOOST_CHECK_EQUAL(pow(T(0), 0), 1); BOOST_CHECK_EQUAL(pow(T(0), T(2)), 0); BOOST_CHECK_EQUAL(pow(T(0), 2), 0); BOOST_CHECK_EQUAL(pow(T(1), T(0)), 1); BOOST_CHECK_EQUAL(pow(T(1), 0), 1); BOOST_CHECK_EQUAL(pow(T(1), T(2)), 1); BOOST_CHECK_EQUAL(pow(T(1), 2), 1); BOOST_IF_CONSTEXPR(std::numeric_limits::is_specialized && std::numeric_limits::has_infinity) { BOOST_CHECK_EQUAL(pow(T(1.25F), std::numeric_limits::infinity()), std::numeric_limits::infinity()); BOOST_CHECK_EQUAL(pow(T(1.25F), -std::numeric_limits::infinity()), T(0)); } BOOST_IF_CONSTEXPR((!boost::multiprecision::is_interval_number::value) && std::numeric_limits::is_specialized && std::numeric_limits::has_quiet_NaN) { BOOST_CHECK((boost::math::isnan)(pow(T(1.25F), std::numeric_limits::quiet_NaN()))); BOOST_CHECK_EQUAL(pow(std::numeric_limits::quiet_NaN(), T(0)), T(1)); BOOST_CHECK((boost::math::isnan)(pow(std::numeric_limits::quiet_NaN(), T(1.25F)))); } if (!boost::multiprecision::is_interval_number::value) { T bug_case = -1.05 * log((std::numeric_limits::max)()) / log(T(1.01)); for (unsigned i = 0; i < 100; ++i, bug_case *= 1.05) { if (std::numeric_limits::has_infinity) { BOOST_CHECK_EQUAL(pow(T(1.01), bug_case), 0); } else { BOOST_CHECK_LE(pow(T(1.01), bug_case), (std::numeric_limits::min)()); } } } } int main() { #ifdef TEST_BACKEND test >(); #endif #ifdef TEST_MPF_50 test(); test(); #endif #ifdef TEST_MPFR_50 test(); test(); #endif #ifdef TEST_MPFI_50 test(); test(); #endif #ifdef TEST_CPP_DEC_FLOAT test(); test(); #ifndef SLOW_COMPLER // Some "peculiar" digit counts which stress our code: test > >(); test > >(); test > >(); test > >(); test > >(); test > >(); test > > >(); test > > >(); // Check low multiprecision digit counts. test > >(); test > >(); #endif #endif #ifdef TEST_FLOAT128 test(); #endif #ifdef TEST_CPP_BIN_FLOAT test(); test, long long> > >(); #endif #ifdef TEST_CPP_DOUBLE_FLOAT test(); test(); #if defined(BOOST_MP_CPP_DOUBLE_FP_HAS_FLOAT128) test(); #endif #endif return boost::report_errors(); }