|
| 1 | +// Copyright 2024 Matt Borland |
| 2 | +// Copyright 2024 Christopher Kormanyos |
| 3 | +// Distributed under the Boost Software License, Version 1.0. |
| 4 | +// https://www.boost.org/LICENSE_1_0.txt |
| 5 | + |
| 6 | +#include <chrono> |
| 7 | +#include <iomanip> |
| 8 | +#include <iostream> |
| 9 | +#include <limits> |
| 10 | +#include <random> |
| 11 | +#include <string> |
| 12 | + |
| 13 | +#include <boost/decimal.hpp> |
| 14 | + |
| 15 | +#if defined(__clang__) |
| 16 | +# pragma clang diagnostic push |
| 17 | +# pragma clang diagnostic ignored "-Wfloat-equal" |
| 18 | +#elif defined(__GNUC__) |
| 19 | +# pragma GCC diagnostic push |
| 20 | +# pragma GCC diagnostic ignored "-Wfloat-equal" |
| 21 | +#endif |
| 22 | + |
| 23 | +#include <boost/core/lightweight_test.hpp> |
| 24 | + |
| 25 | +namespace local |
| 26 | +{ |
| 27 | + template<typename IntegralTimePointType, |
| 28 | + typename ClockType = std::chrono::high_resolution_clock> |
| 29 | + auto time_point() noexcept -> IntegralTimePointType |
| 30 | + { |
| 31 | + using local_integral_time_point_type = IntegralTimePointType; |
| 32 | + using local_clock_type = ClockType; |
| 33 | + |
| 34 | + const auto current_now = |
| 35 | + static_cast<std::uintmax_t> |
| 36 | + ( |
| 37 | + std::chrono::duration_cast<std::chrono::nanoseconds> |
| 38 | + ( |
| 39 | + local_clock_type::now().time_since_epoch() |
| 40 | + ).count() |
| 41 | + ); |
| 42 | + |
| 43 | + return static_cast<local_integral_time_point_type>(current_now); |
| 44 | + } |
| 45 | + |
| 46 | + template<typename NumericType> |
| 47 | + auto is_close_fraction(const NumericType& a, |
| 48 | + const NumericType& b, |
| 49 | + const NumericType& tol) noexcept -> bool |
| 50 | + { |
| 51 | + using std::fabs; |
| 52 | + |
| 53 | + auto result_is_ok = bool { }; |
| 54 | + |
| 55 | + NumericType delta { }; |
| 56 | + |
| 57 | + if(b == static_cast<NumericType>(0)) |
| 58 | + { |
| 59 | + delta = fabs(a - b); // LCOV_EXCL_LINE |
| 60 | + |
| 61 | + result_is_ok = (delta < tol); // LCOV_EXCL_LINE |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + delta = fabs(1 - (a / b)); |
| 66 | + |
| 67 | + result_is_ok = (delta < tol); |
| 68 | + } |
| 69 | + |
| 70 | + // LCOV_EXCL_START |
| 71 | + if (!result_is_ok) |
| 72 | + { |
| 73 | + std::cerr << std::setprecision(std::numeric_limits<NumericType>::digits10) << "a: " << a |
| 74 | + << "\nb: " << b |
| 75 | + << "\ndelta: " << delta |
| 76 | + << "\ntol: " << tol << std::endl; |
| 77 | + } |
| 78 | + // LCOV_EXCL_STOP |
| 79 | + |
| 80 | + return result_is_ok; |
| 81 | + } |
| 82 | + |
| 83 | + template<typename DecimalType, typename FloatType> |
| 84 | + auto test_log10(const int tol_factor) -> bool |
| 85 | + { |
| 86 | + using decimal_type = DecimalType; |
| 87 | + using float_type = FloatType; |
| 88 | + |
| 89 | + std::random_device rd; |
| 90 | + std::mt19937_64 gen(rd()); |
| 91 | + |
| 92 | + gen.seed(time_point<typename std::mt19937_64::result_type>()); |
| 93 | + |
| 94 | + auto dis_r = |
| 95 | + std::uniform_real_distribution<float_type> |
| 96 | + { |
| 97 | + static_cast<float_type>(1.2L), |
| 98 | + static_cast<float_type>(8.9L) |
| 99 | + }; |
| 100 | + |
| 101 | + bool result_is_ok { true }; |
| 102 | + |
| 103 | + auto trials = static_cast<std::uint32_t>(UINT8_C(0)); |
| 104 | + |
| 105 | + #if !defined(BOOST_DECIMAL_REDUCE_TEST_DEPTH) |
| 106 | + constexpr auto count = (sizeof(decimal_type) == static_cast<std::size_t>(UINT8_C(4))) ? static_cast<std::uint32_t>(UINT32_C(0x200)) : static_cast<std::uint32_t>(UINT32_C(0x40)); |
| 107 | + #else |
| 108 | + constexpr auto count = (sizeof(decimal_type) == static_cast<std::size_t>(UINT8_C(4))) ? static_cast<std::uint32_t>(UINT32_C(0x40)) : static_cast<std::uint32_t>(UINT32_C(0x4)); |
| 109 | + #endif |
| 110 | + |
| 111 | + for( ; trials < count; ++trials) |
| 112 | + { |
| 113 | + auto x_flt = dis_r(gen); |
| 114 | + |
| 115 | + auto dis_n = |
| 116 | + std::uniform_int_distribution<int> |
| 117 | + { |
| 118 | + static_cast<int>(INT8_C(-17)), |
| 119 | + static_cast<int>(INT8_C(17)) |
| 120 | + }; |
| 121 | + |
| 122 | + std::string str_e { "1.0E" + std::to_string(dis_n(gen)) }; |
| 123 | + |
| 124 | + char* p_end; |
| 125 | + |
| 126 | + x_flt *= static_cast<float>(strtold(str_e.c_str(), &p_end)); |
| 127 | + |
| 128 | + static_cast<void>(p_end); |
| 129 | + |
| 130 | + const auto x_dec = static_cast<decimal_type>(x_flt); |
| 131 | + |
| 132 | + using std::log10; |
| 133 | + |
| 134 | + const auto val_flt = log10(x_flt); |
| 135 | + const auto val_dec = log10(x_dec); |
| 136 | + |
| 137 | + const auto result_log_is_ok = is_close_fraction(val_flt, static_cast<float_type>(val_dec), std::numeric_limits<float_type>::epsilon() * static_cast<float_type>(tol_factor)); |
| 138 | + |
| 139 | + result_is_ok = (result_log_is_ok && result_is_ok); |
| 140 | + |
| 141 | + if(!result_log_is_ok) |
| 142 | + { |
| 143 | + // LCOV_EXCL_START |
| 144 | + std::cerr << "x_flt : " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << x_flt << std::endl; |
| 145 | + std::cerr << "val_flt: " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << val_flt << std::endl; |
| 146 | + std::cerr << "val_dec: " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << val_dec << std::endl; |
| 147 | + |
| 148 | + break; |
| 149 | + // LCOV_EXCL_STOP |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + BOOST_TEST(result_is_ok); |
| 154 | + |
| 155 | + return result_is_ok; |
| 156 | + } |
| 157 | + |
| 158 | + template<typename DecimalType, typename FloatType> |
| 159 | + auto test_log10_pow10(const int tol_factor) -> bool |
| 160 | + { |
| 161 | + using decimal_type = DecimalType; |
| 162 | + using float_type = FloatType; |
| 163 | + |
| 164 | + bool result_is_ok { true }; |
| 165 | + |
| 166 | + for(int i = static_cast<int>(INT8_C(-23)); i <= static_cast<int>(INT8_C(23)); ++i) |
| 167 | + { |
| 168 | + const decimal_type x_arg { 1, i }; |
| 169 | + |
| 170 | + const decimal_type log_val_p10 = log10(x_arg); |
| 171 | + |
| 172 | + const auto result_log10_pow10_is_ok = (static_cast<float_type>(log_val_p10) == static_cast<float_type>(i)); |
| 173 | + |
| 174 | + result_is_ok = (result_log10_pow10_is_ok && result_is_ok); |
| 175 | + } |
| 176 | + |
| 177 | + BOOST_TEST(result_is_ok); |
| 178 | + |
| 179 | + return result_is_ok; |
| 180 | + } |
| 181 | + |
| 182 | +} // namespace local |
| 183 | + |
| 184 | +auto main() -> int |
| 185 | +{ |
| 186 | + auto result_is_ok = true; |
| 187 | + |
| 188 | + { |
| 189 | + using decimal_type = boost::decimal::decimal32; |
| 190 | + using float_type = float; |
| 191 | + |
| 192 | + const auto test_log10_is_ok = local::test_log10<decimal_type, float_type>(128); |
| 193 | + |
| 194 | + result_is_ok = (test_log10_is_ok && result_is_ok); |
| 195 | + } |
| 196 | + |
| 197 | + result_is_ok = ((boost::report_errors() == 0) && result_is_ok); |
| 198 | + |
| 199 | + return (result_is_ok ? 0 : -1); |
| 200 | +} |
0 commit comments