Skip to content

Commit 7fdcf98

Browse files
committed
Tolerances and typos
1 parent 6102696 commit 7fdcf98

File tree

4 files changed

+120
-46
lines changed

4 files changed

+120
-46
lines changed

include/boost/decimal/detail/cmath/impl/tgamma_impl.hpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ namespace tgamma_detail {
2424
template <bool b>
2525
struct tgamma_table_imp
2626
{
27-
static constexpr std::array<decimal32, 15> d32_coeffs =
27+
private:
28+
using d32_coeffs_t = std::array<decimal32, 15>;
29+
using d64_coeffs_t = std::array<decimal64, 36>;
30+
using d128_coeffs_t = std::array<decimal128, 44>;
31+
32+
public:
33+
static constexpr d32_coeffs_t d32_coeffs =
2834
{{
29-
// N[Series[1/Gamma[z], {z, 0, 16}], 24]
35+
// N[Series[1/Gamma[z], {z, 0, 16}], 19]
3036
+::boost::decimal::decimal32 { UINT64_C(5'772'156'649'015'328'606), - 19 - 0 }, // * z^2
3137
-::boost::decimal::decimal32 { UINT64_C(6'558'780'715'202'538'811), - 19 - 0 }, // * z^3
3238
-::boost::decimal::decimal32 { UINT64_C(4'200'263'503'409'523'553), - 19 - 1 }, // * z^4
@@ -44,9 +50,9 @@ struct tgamma_table_imp
4450
+::boost::decimal::decimal32 { UINT64_C(6'116'095'104'481'415'818), - 19 - 8 }, // * z^16
4551
}};
4652

47-
static constexpr std::array<decimal64, 36> d64_coeffs =
53+
static constexpr d64_coeffs_t d64_coeffs =
4854
{{
49-
// N[Series[1/Gamma[z], {z, 0, 27}], 24]
55+
// N[Series[1/Gamma[z], {z, 0, 27}], 19]
5056
+::boost::decimal::decimal64 { UINT64_C(5'772'156'649'015'328'606), - 19 - 0 }, // * z^2
5157
-::boost::decimal::decimal64 { UINT64_C(6'558'780'715'202'538'811), - 19 - 0 }, // * z^3
5258
-::boost::decimal::decimal64 { UINT64_C(4'200'263'503'409'523'553), - 19 - 1 }, // * z^4
@@ -75,9 +81,9 @@ struct tgamma_table_imp
7581
+::boost::decimal::decimal64 { UINT64_C(1'186'692'254'751'600'333), - 19 - 17 }, // * z^27
7682
}};
7783

78-
static constexpr std::array<decimal128, 44> d128_coeffs =
84+
static constexpr d128_coeffs_t d128_coeffs =
7985
{{
80-
// N[Series[1/Gamma[z], {z, 0, 27}], 24]
86+
// N[Series[1/Gamma[z], {z, 0, 45}], 36]
8187
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(312909238939453), UINT64_C(7916302232898517972) }, -34 }, // * z^2
8288
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(355552215013931), UINT64_C(2875353717947891404) }, -34 }, // * z^3
8389
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(227696740770409), UINT64_C(1287992959696612036) }, -35 }, // * z^4
@@ -104,24 +110,24 @@ struct tgamma_table_imp
104110
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(66503802694735), UINT64_C(8619931044472680662) }, -48 }, // * z^25
105111
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(64036195058454), UINT64_C(13570784405336680634) }, -49 }, // * z^26
106112
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(64330716033670), UINT64_C(6228121739584017954) }, -51 }, // * z^27
107-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(76565308743615), UINT64_C(9665163337994634860) }, -51 },
108-
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(124615253252825), UINT64_C(5713012462345318490) }, -52 },
109-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(92938152937825), UINT64_C(2160517649493992050) }, -53 },
110-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(72497982578925), UINT64_C(10055707640313829460) }, -55 },
111-
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(111360223980902), UINT64_C(528747408384118098) }, -55 },
112-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(148320486134320), UINT64_C(12662323637555269860) }, -56 },
113-
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(93911231108772), UINT64_C(8663955293807189228) }, -57 },
114-
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(127969413738636), UINT64_C(17978922200959991754) }, -59 },
115-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(101100927852914), UINT64_C(16158702556622869636) }, -59 },
116-
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(120243204727301), UINT64_C(13141135468649758444) }, -60 },
117-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(70352901832557), UINT64_C(2975454173305568482) }, -61 },
118-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(64005738370342), UINT64_C(18063645830042937300) }, -63 },
119-
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(60963839731470), UINT64_C(14965217315129705920) }, -63 },
120-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(69230926066837), UINT64_C(16656915204960392533) }, -64 },
121-
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(400691370795862), UINT64_C(16972369904241895558) }, -66 },
122-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(61514934723438), UINT64_C(5918930041313493498) }, -68 },
123-
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(251487992814431), UINT64_C(6680121266003781724) }, -68 },
124-
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(289879709778175), UINT64_C(4432551928123929090) }, -69 },
113+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(76565308743615), UINT64_C(9665163337994634860) }, -51 }, // * z^28
114+
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(124615253252825), UINT64_C(5713012462345318490) }, -52 }, // * z^29
115+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(92938152937825), UINT64_C(2160517649493992050) }, -53 }, // * z^30
116+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(72497982578925), UINT64_C(10055707640313829460) }, -55 }, // * z^31
117+
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(111360223980902), UINT64_C(528747408384118098) }, -55 }, // * z^32
118+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(148320486134320), UINT64_C(12662323637555269860) }, -56 }, // * z^33
119+
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(93911231108772), UINT64_C(8663955293807189228) }, -57 }, // * z^34
120+
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(127969413738636), UINT64_C(17978922200959991754) }, -59 }, // * z^35
121+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(101100927852914), UINT64_C(16158702556622869636) }, -59 }, // * z^36
122+
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(120243204727301), UINT64_C(13141135468649758444) }, -60 }, // * z^37
123+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(70352901832557), UINT64_C(2975454173305568482) }, -61 }, // * z^38
124+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(64005738370342), UINT64_C(18063645830042937300) }, -63 }, // * z^39
125+
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(60963839731470), UINT64_C(14965217315129705920) }, -63 }, // * z^40
126+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(69230926066837), UINT64_C(16656915204960392533) }, -64 }, // * z^41
127+
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(400691370795862), UINT64_C(16972369904241895558) }, -66 }, // * z^42
128+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(61514934723438), UINT64_C(5918930041313493498) }, -68 }, // * z^43
129+
+::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(251487992814431), UINT64_C(6680121266003781724) }, -68 }, // * z^44
130+
-::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(289879709778175), UINT64_C(4432551928123929090) }, -69 }, // * z^45
125131
}};
126132
};
127133

test/test_exp.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,7 @@ auto main() -> int
378378
}
379379

380380
{
381-
using decimal_type = boost::decimal::decimal128;
382-
383-
const auto result_pos128_is_ok = local::test_exp_128(400000);
381+
const auto result_pos128_is_ok = local::test_exp_128(400'000);
384382

385383
BOOST_TEST(result_pos128_is_ok);
386384

test/test_tanh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ auto main() -> int
352352

353353
const auto result_pos64_is_ok = local::test_tanh_64(64);
354354

355-
const auto result_pos128_is_ok = local::test_tanh_128(400000);
355+
const auto result_pos128_is_ok = local::test_tanh_128(400'000);
356356

357357
BOOST_TEST(result_pos_is_ok);
358358
BOOST_TEST(result_neg_is_ok);

test/test_tgamma.cpp

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,30 @@ namespace local
5454

5555
auto result_is_ok = bool { };
5656

57+
NumericType delta { };
58+
5759
if(b == static_cast<NumericType>(0))
5860
{
59-
result_is_ok = (fabs(a - b) < tol);
61+
delta = fabs(a - b); // LCOV_EXCL_LINE
62+
63+
result_is_ok = (delta < tol); // LCOV_EXCL_LINE
6064
}
6165
else
6266
{
63-
const auto delta = fabs(1 - (a / b));
67+
delta = fabs(1 - (a / b));
6468

6569
result_is_ok = (delta < tol);
70+
}
6671

67-
if (!result_is_ok)
68-
{
69-
std::cerr << std::setprecision(std::numeric_limits<NumericType>::digits10) << "a: " << a
70-
<< "\nb: " << b
71-
<< "\ndelta: " << delta
72-
<< "\ntol: " << tol << std::endl;
73-
}
72+
// LCOV_EXCL_START
73+
if (!result_is_ok)
74+
{
75+
std::cerr << std::setprecision(std::numeric_limits<NumericType>::digits10) << "a: " << a
76+
<< "\nb: " << b
77+
<< "\ndelta: " << delta
78+
<< "\ntol: " << tol << std::endl;
7479
}
80+
// LCOV_EXCL_STOP
7581

7682
return result_is_ok;
7783
}
@@ -121,9 +127,9 @@ namespace local
121127
if(!result_val_is_ok)
122128
{
123129
// LCOV_EXCL_START
124-
std::cout << "x_flt : " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << x_flt << std::endl;
125-
std::cout << "val_flt: " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << val_flt << std::endl;
126-
std::cout << "val_dec: " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << val_dec << std::endl;
130+
std::cerr << "x_flt : " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << x_flt << std::endl;
131+
std::cerr << "val_flt: " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << val_flt << std::endl;
132+
std::cerr << "val_dec: " << std::scientific << std::setprecision(std::numeric_limits<float_type>::digits10) << val_dec << std::endl;
127133

128134
break;
129135
// LCOV_EXCL_STOP
@@ -325,6 +331,60 @@ namespace local
325331
return result_is_ok;
326332
}
327333

334+
auto test_tgamma_64(const int tol_factor) -> bool
335+
{
336+
using decimal_type = boost::decimal::decimal64;
337+
338+
using val_ctrl_array_type = std::array<double, 9U>;
339+
340+
const val_ctrl_array_type ctrl_values =
341+
{{
342+
// Table[N[Gamma[(100 n + 10 n + 1)/100], 17], {n, 1, 9, 1}]
343+
0.94739550403930194,
344+
1.1078475565340642,
345+
2.7113982392439032,
346+
10.275404092015205,
347+
53.193428252500821,
348+
350.99860982420059,
349+
2825.0945368041871,
350+
26903.671946749768,
351+
296439.08210247219
352+
}};
353+
354+
std::array<decimal_type, std::tuple_size<val_ctrl_array_type>::value> tgamma_values { };
355+
356+
int nx { 1 };
357+
358+
bool result_is_ok { true };
359+
360+
const decimal_type my_tol { std::numeric_limits<decimal_type>::epsilon() * static_cast<decimal_type>(tol_factor) };
361+
362+
for(auto i = static_cast<std::size_t>(UINT8_C(0)); i < std::tuple_size<val_ctrl_array_type>::value; ++i)
363+
{
364+
// Table[N[Gamma[(100 n + 10 n + 1)/100], 17], {n, 1, 9, 1}]
365+
366+
const decimal_type x_arg =
367+
decimal_type
368+
{
369+
decimal_type { 1, 2 } * nx
370+
+ decimal_type { 1, 1 } * nx
371+
+ 1
372+
}
373+
/ decimal_type { 1, 2 };
374+
375+
376+
tgamma_values[i] = tgamma(x_arg);
377+
378+
++nx;
379+
380+
const auto result_tgamma_is_ok = is_close_fraction(tgamma_values[i], decimal_type(ctrl_values[i]), my_tol);
381+
382+
result_is_ok = (result_tgamma_is_ok && result_is_ok);
383+
}
384+
385+
return result_is_ok;
386+
}
387+
328388
auto test_tgamma_128(const int tol_factor) -> bool
329389
{
330390
using decimal_type = boost::decimal::decimal128;
@@ -348,24 +408,24 @@ namespace local
348408
std::array<decimal_type, std::tuple_size<str_ctrl_array_type>::value> tg_values { };
349409
std::array<decimal_type, std::tuple_size<str_ctrl_array_type>::value> ctrl_values { };
350410

351-
int n { 1 };
411+
int nx { 1 };
352412

353413
bool result_is_ok { true };
354414

355415
const decimal_type my_tol { std::numeric_limits<decimal_type>::epsilon() * static_cast<decimal_type>(tol_factor) };
356416

357417
for(auto i = static_cast<std::size_t>(UINT8_C(0)); i < std::tuple_size<str_ctrl_array_type>::value; ++i)
358418
{
359-
decimal_type x_arg =
419+
const decimal_type x_arg =
360420
decimal_type
361421
{
362-
decimal_type { 1, 2 } * n
363-
+ decimal_type { 1, 1 } * n
422+
decimal_type { 1, 2 } * nx
423+
+ decimal_type { 1, 1 } * nx
364424
+ 1
365425
}
366426
/ decimal_type { 1, 2 };
367427

368-
++n;
428+
++nx;
369429

370430
tg_values[i] = tgamma(x_arg);
371431

@@ -438,7 +498,17 @@ auto main() -> int
438498
}
439499

440500
{
441-
const auto result_tgamma128_is_ok = local::test_tgamma_128(1000000);
501+
const auto result_tgamma64_is_ok = local::test_tgamma_64(128);
502+
503+
BOOST_TEST(result_tgamma64_is_ok);
504+
505+
result_is_ok = (result_tgamma64_is_ok && result_is_ok);
506+
}
507+
508+
{
509+
// TODO(ckormanyos) Can the accuracy/precision of tgamma-128 be improved?
510+
511+
const auto result_tgamma128_is_ok = local::test_tgamma_128(800'000);
442512

443513
BOOST_TEST(result_tgamma128_is_ok);
444514

0 commit comments

Comments
 (0)