1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
// Copyright 2024 Christopher Kormanyos
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/math/special_functions/gamma.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <random>
namespace local
{
std::mt19937 eng(static_cast<typename std::mt19937::result_type>(UINT8_C(42)));
std::uniform_int_distribution<int> dst_one(1, 1);
template<typename NumericType>
auto is_close_fraction(const NumericType& a,
const NumericType& b,
const NumericType& tol) noexcept -> bool
{
using std::fabs;
auto result_is_ok = bool { };
if(b == static_cast<NumericType>(0))
{
result_is_ok = (fabs(a - b) < tol);
}
else
{
const auto delta = fabs(1 - (a / b));
result_is_ok = (delta < tol);
}
return result_is_ok;
}
auto tgamma_under_cbrt_epsilon() -> void
{
// This test is intended to hit the lines:
// template <class T, class Policy>
// T gamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&)
// ...
// ...near the comment:
// Special case for ultra-small z:
using local_float_type = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<250>, boost::multiprecision::et_off>;
static_assert( (std::numeric_limits<local_float_type>::digits10 >= 248)
&& (std::numeric_limits<local_float_type>::digits10 <= 252), "Error: Multiprecision wrong number of digits");
// Table[N[Gamma[n (10^-84)], 260], {n, 1, 10, 1}]
using local_data_array_type = std::array<local_float_type, static_cast<std::size_t>(UINT8_C(10))>;
const local_data_array_type ctrl_data =
{{
static_cast<local_float_type>("9.9999999999999999999999999999999999999999999999999999999999999999999999999999999999942278433509846713939348790991759756895784066406007640119423276511513227322233532906404199270358122304076394840169355222697867950624167125421902178604121309579597843800360126781E+83"),
static_cast<local_float_type>("4.9999999999999999999999999999999999999999999999999999999999999999999999999999999999942278433509846713939348790991759756895784066406007640119423276511513227322233532906503104869890919559615934405319418693491786302696988534466221756972734972784545720974832491521E+83"),
static_cast<local_float_type>("3.3333333333333333333333333333333333333333333333333333333333333333333333333333333333275611766843180047272682124325093090229117399739340973452756609844846560655566866239935343802757050148488807303802815497619037988103143276843874668674681969322826931482456693779E+83"),
static_cast<local_float_type>("2.4999999999999999999999999999999999999999999999999999999999999999999999999999999999942278433509846713939348790991759756895784066406007640119423276511513227322233532906700916068956514070695013535619545635079623006842631352554860913709962299194441475323232733555E+83"),
static_cast<local_float_type>("1.9999999999999999999999999999999999999999999999999999999999999999999999999999999999942278433509846713939348790991759756895784066406007640119423276511513227322233532906799821668489311326234553100769609105873541358915452761599180492078575962399389352497160610849E+83"),
static_cast<local_float_type>("1.6666666666666666666666666666666666666666666666666666666666666666666666666666666666608945100176513380606015457658426423562450733072674306786089943178179893988900199573565393934688775248440759332586339243334126377654940837310166737113856292271003896337573658994E+83"),
static_cast<local_float_type>("1.4285714285714285714285714285714285714285714285714285714285714285714285714285714285656564147795560999653634505277474042610069780691721925833708990797227513036519247192711918581840620123027917945355450333175663777346809865402105363101517574523570821130186163706E+83"),
static_cast<local_float_type>("1.2499999999999999999999999999999999999999999999999999999999999999999999999999999999942278433509846713939348790991759756895784066406007640119423276511513227322233532907096538467087703092853171796219799518255296415133916988732139227184416952014232984017855267840E+83"),
static_cast<local_float_type>("1.1111111111111111111111111111111111111111111111111111111111111111111111111111111111053389544620957825050459902102870868006895177517118751230534387622624338433344644018306555177731611459503822472480974100160325878317849508887569916664141726330291972302168272984E+83"),
static_cast<local_float_type>("9.9999999999999999999999999999999999999999999999999999999999999999999999999999999999422784335098467139393487909917597568957840664060076401194232765115132273222335329072943496661532976039322509265199264598431331192795598068207783839216442784241287383640775600912E+82"),
}};
unsigned index = 1U;
const local_float_type little { "1E-84" };
const local_float_type my_tol { std::numeric_limits<local_float_type>::epsilon() * 256 };
for(const auto& ctrl : ctrl_data)
{
const auto x_small = static_cast<local_float_type>(static_cast<local_float_type>(index) * little);
++index;
const auto g_val = boost::math::tgamma(x_small);
const auto result_tgamma_x_small_is_ok = is_close_fraction(g_val, ctrl, my_tol);
BOOST_TEST(result_tgamma_x_small_is_ok);
if(!result_tgamma_x_small_is_ok)
{
break; // LCOV_EXCL_LINE
}
}
}
auto tgamma_undefined_lanczos_known_error() -> void
{
// This test is intended to hit the lines:
// template <class T, class Policy>
// T gamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&)
// ...
// ...for edge cases that raise errors such as domain error.
using local_float_type = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<250>, boost::multiprecision::et_off>;
{
const local_float_type my_tol { std::numeric_limits<local_float_type>::epsilon() * 256 };
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(3)); ++index)
{
static_cast<void>(index);
const local_float_type zero_ctrl { 0 };
local_float_type zero { 0 };
zero *= dst_one(eng);
const auto result_zero_is_ok = is_close_fraction(zero, zero_ctrl, my_tol);
BOOST_TEST(result_zero_is_ok);
}
}
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(3)); ++index)
{
static_cast<void>(index);
local_float_type zero { 0 };
zero *= dst_one(eng);
bool domain_error_is_ok { false };
try
{
boost::math::tgamma(zero);
}
catch(std::domain_error& err)
{
static_cast<void>(err.what());
domain_error_is_ok = true;
}
BOOST_TEST(domain_error_is_ok);
}
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(3)); ++index)
{
static_cast<void>(index);
local_float_type my_nan = std::numeric_limits<local_float_type>::quiet_NaN();
my_nan *= dst_one(eng);
bool domain_error_is_ok { false };
try
{
boost::math::tgamma(my_nan);
}
catch(std::domain_error& err)
{
static_cast<void>(err.what());
domain_error_is_ok = true;
}
BOOST_TEST(domain_error_is_ok);
}
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(3)); ++index)
{
static_cast<void>(index);
local_float_type my_inf = -std::numeric_limits<local_float_type>::infinity();
my_inf *= dst_one(eng);
bool domain_error_is_ok { false };
try
{
boost::math::tgamma(my_inf);
}
catch(std::domain_error& err)
{
static_cast<void>(err.what());
domain_error_is_ok = true;
}
BOOST_TEST(domain_error_is_ok);
}
}
auto lgamma_big_asymp() -> void
{
// This test is intended to hit the asymptotic log-gamma expansion for multiprecision.
using local_float_type = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<250>, boost::multiprecision::et_off>;
static_assert( (std::numeric_limits<local_float_type>::digits10 >= 248)
&& (std::numeric_limits<local_float_type>::digits10 <= 252), "Error: Multiprecision wrong number of digits");
local_float_type big_arg_numerator { 1234567L };
// Table[N[Log[Gamma[(1234567 + n)/1000]], 260], {n, 0, 3, 1}]
const local_float_type ctrl0 { "7551.0278099842760398085493506933061185258592164059260052791257174648102458654516760859347475429811747227042884941464597963128452844941163716092798494933305452087249880911022309522317482008162381529082884245980549740815352929296384544778543502768128060636123031" };
const local_float_type ctrl1 { "7551.0349280552065308610373629214633349814110368633190642156085097598877230874250481117271260334496206128158535271616589000730715715804390525860149840442193710637326207809853649225510544815601053550751028151966244578864039961973124357117676870769851159530881598" };
const local_float_type ctrl2 { "7551.0420461269473499872464481408311466395059218944034402487444941839649424484440812974771163653823079980811183096059838617975730358237216549153177603277276543261570202589028877022053234067330484829335430150182956719650949866427225508960918047040961544342635607" };
const local_float_type ctrl3 { "7551.0491641994984965305455873077287163417673805051480753266140277293097406691212685736787897808049319210175552316063915681371903149757892763598947756743285838029800201937242960777121582844482027617050641997959480250230315703285501155035159657216828260204447321" };
const local_float_type my_tol { std::numeric_limits<local_float_type>::epsilon() * 256 };
const local_float_type lg_big0 { boost::math::lgamma(big_arg_numerator / 1000) }; ++big_arg_numerator;
const local_float_type lg_big1 { boost::math::lgamma(big_arg_numerator / 1000) }; ++big_arg_numerator;
const local_float_type lg_big2 { boost::math::lgamma(big_arg_numerator / 1000) }; ++big_arg_numerator;
const local_float_type lg_big3 { boost::math::lgamma(big_arg_numerator / 1000) };
const auto result_lgamma_big0_is_ok = is_close_fraction(lg_big0, ctrl0, my_tol);
const auto result_lgamma_big1_is_ok = is_close_fraction(lg_big1, ctrl1, my_tol);
const auto result_lgamma_big2_is_ok = is_close_fraction(lg_big2, ctrl2, my_tol);
const auto result_lgamma_big3_is_ok = is_close_fraction(lg_big3, ctrl3, my_tol);
BOOST_TEST(result_lgamma_big0_is_ok);
BOOST_TEST(result_lgamma_big1_is_ok);
BOOST_TEST(result_lgamma_big2_is_ok);
BOOST_TEST(result_lgamma_big3_is_ok);
}
auto lgamma_undefined_lanczos_known_error() -> void
{
// This test is intended to hit the lines:
// template <class T, class Policy>
// T lgamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&, int* sign) // ...
// ...
// ...for edge cases that raise errors such as domain error.
using local_float_type = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<250>, boost::multiprecision::et_off>;
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(3)); ++index)
{
static_cast<void>(index);
local_float_type zero { 0 };
zero *= dst_one(eng);
bool domain_error_is_ok { false };
try
{
boost::math::lgamma(zero);
}
catch(std::domain_error& err)
{
static_cast<void>(err.what());
domain_error_is_ok = true;
}
BOOST_TEST(domain_error_is_ok);
}
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(3)); ++index)
{
static_cast<void>(index);
local_float_type my_nan = std::numeric_limits<local_float_type>::quiet_NaN();
my_nan *= dst_one(eng);
bool domain_error_is_ok { false };
try
{
boost::math::lgamma(my_nan);
}
catch(std::domain_error& err)
{
static_cast<void>(err.what());
domain_error_is_ok = true;
}
BOOST_TEST(domain_error_is_ok);
}
for(auto index = static_cast<unsigned>(UINT8_C(0)); index < static_cast<unsigned>(UINT8_C(3)); ++index)
{
static_cast<void>(index);
local_float_type my_inf = -std::numeric_limits<local_float_type>::infinity();
my_inf *= dst_one(eng);
bool overflow_error_is_ok { false };
try
{
boost::math::lgamma(my_inf);
}
catch(std::overflow_error& err)
{
static_cast<void>(err.what());
overflow_error_is_ok = true;
}
BOOST_TEST(overflow_error_is_ok);
}
}
}
auto main() -> int
{
local::tgamma_under_cbrt_epsilon();
local::tgamma_undefined_lanczos_known_error();
local::lgamma_big_asymp();
local::lgamma_undefined_lanczos_known_error();
const auto result_is_ok = (boost::report_errors() == 0);
return (result_is_ok ? 0 : -1);
}
|