lgamma_small.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. // (C) Copyright John Maddock 2006.
  2. // (C) Copyright Matt Borland 2024.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_SPECIAL_FUNCTIONS_DETAIL_LGAMMA_SMALL
  7. #define BOOST_MATH_SPECIAL_FUNCTIONS_DETAIL_LGAMMA_SMALL
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #endif
  11. #include <boost/math/tools/config.hpp>
  12. #include <boost/math/tools/big_constant.hpp>
  13. #include <boost/math/tools/type_traits.hpp>
  14. #include <boost/math/tools/precision.hpp>
  15. #include <boost/math/special_functions/lanczos.hpp>
  16. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  17. //
  18. // This is the only way we can avoid
  19. // warning: non-standard suffix on floating constant [-Wpedantic]
  20. // when building with -Wall -pedantic. Neither __extension__
  21. // nor #pragma diagnostic ignored work :(
  22. //
  23. #pragma GCC system_header
  24. #endif
  25. namespace boost{ namespace math{ namespace detail{
  26. //
  27. // These need forward declaring to keep GCC happy:
  28. //
  29. template <class T, class Policy, class Lanczos>
  30. BOOST_MATH_GPU_ENABLED T gamma_imp(T z, const Policy& pol, const Lanczos& l);
  31. template <class T, class Policy>
  32. BOOST_MATH_GPU_ENABLED T gamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos& l);
  33. //
  34. // lgamma for small arguments:
  35. //
  36. template <class T, class Policy, class Lanczos>
  37. BOOST_MATH_GPU_ENABLED T lgamma_small_imp(T z, T zm1, T zm2, const boost::math::integral_constant<int, 64>&, const Policy& /* l */, const Lanczos&)
  38. {
  39. // This version uses rational approximations for small
  40. // values of z accurate enough for 64-bit mantissas
  41. // (80-bit long doubles), works well for 53-bit doubles as well.
  42. // Lanczos is only used to select the Lanczos function.
  43. BOOST_MATH_STD_USING // for ADL of std names
  44. T result = 0;
  45. BOOST_MATH_ASSERT(z >= tools::root_epsilon<T>());
  46. /*
  47. * Can not be reached:
  48. *
  49. if(z < tools::epsilon<T>())
  50. {
  51. result = -log(z);
  52. }
  53. */
  54. if((zm1 == 0) || (zm2 == 0))
  55. {
  56. // nothing to do, result is zero....
  57. }
  58. else if(z > 2)
  59. {
  60. //
  61. // Begin by performing argument reduction until
  62. // z is in [2,3):
  63. //
  64. if(z >= 3)
  65. {
  66. do
  67. {
  68. z -= 1;
  69. zm2 -= 1;
  70. result += log(z);
  71. }while(z >= 3);
  72. // Update zm2, we need it below:
  73. zm2 = z - 2;
  74. }
  75. //
  76. // Use the following form:
  77. //
  78. // lgamma(z) = (z-2)(z+1)(Y + R(z-2))
  79. //
  80. // where R(z-2) is a rational approximation optimised for
  81. // low absolute error - as long as it's absolute error
  82. // is small compared to the constant Y - then any rounding
  83. // error in it's computation will get wiped out.
  84. //
  85. // R(z-2) has the following properties:
  86. //
  87. // At double: Max error found: 4.231e-18
  88. // At long double: Max error found: 1.987e-21
  89. // Maximum Deviation Found (approximation error): 5.900e-24
  90. //
  91. // LCOV_EXCL_START
  92. BOOST_MATH_STATIC const T P[] = {
  93. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.180355685678449379109e-1)),
  94. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.25126649619989678683e-1)),
  95. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.494103151567532234274e-1)),
  96. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.172491608709613993966e-1)),
  97. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.259453563205438108893e-3)),
  98. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.541009869215204396339e-3)),
  99. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.324588649825948492091e-4))
  100. };
  101. BOOST_MATH_STATIC const T Q[] = {
  102. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.1e1)),
  103. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.196202987197795200688e1)),
  104. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.148019669424231326694e1)),
  105. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.541391432071720958364e0)),
  106. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.988504251128010129477e-1)),
  107. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.82130967464889339326e-2)),
  108. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.224936291922115757597e-3)),
  109. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.223352763208617092964e-6))
  110. };
  111. // LCOV_EXCL_STOP
  112. constexpr float Y = 0.158963680267333984375e0f;
  113. T r = zm2 * (z + 1);
  114. T R = tools::evaluate_polynomial(P, zm2);
  115. R /= tools::evaluate_polynomial(Q, zm2);
  116. result += r * Y + r * R;
  117. }
  118. else
  119. {
  120. //
  121. // If z is less than 1 use recurrence to shift to
  122. // z in the interval [1,2]:
  123. //
  124. if(z < 1)
  125. {
  126. result += -log(z);
  127. zm2 = zm1;
  128. zm1 = z;
  129. z += 1;
  130. }
  131. //
  132. // Two approximations, on for z in [1,1.5] and
  133. // one for z in [1.5,2]:
  134. //
  135. if(z <= T(1.5))
  136. {
  137. //
  138. // Use the following form:
  139. //
  140. // lgamma(z) = (z-1)(z-2)(Y + R(z-1))
  141. //
  142. // where R(z-1) is a rational approximation optimised for
  143. // low absolute error - as long as it's absolute error
  144. // is small compared to the constant Y - then any rounding
  145. // error in it's computation will get wiped out.
  146. //
  147. // R(z-1) has the following properties:
  148. //
  149. // At double precision: Max error found: 1.230011e-17
  150. // At 80-bit long double precision: Max error found: 5.631355e-21
  151. // Maximum Deviation Found: 3.139e-021
  152. // Expected Error Term: 3.139e-021
  153. // LCOV_EXCL_START
  154. constexpr float Y = 0.52815341949462890625f;
  155. BOOST_MATH_STATIC const T P[] = {
  156. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.490622454069039543534e-1)),
  157. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.969117530159521214579e-1)),
  158. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.414983358359495381969e0)),
  159. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.406567124211938417342e0)),
  160. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.158413586390692192217e0)),
  161. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.240149820648571559892e-1)),
  162. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.100346687696279557415e-2))
  163. };
  164. BOOST_MATH_STATIC const T Q[] = {
  165. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.1e1)),
  166. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.302349829846463038743e1)),
  167. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.348739585360723852576e1)),
  168. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.191415588274426679201e1)),
  169. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.507137738614363510846e0)),
  170. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.577039722690451849648e-1)),
  171. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.195768102601107189171e-2))
  172. };
  173. // LCOV_EXCL_STOP
  174. T r = tools::evaluate_polynomial(P, zm1) / tools::evaluate_polynomial(Q, zm1);
  175. T prefix = zm1 * zm2;
  176. result += prefix * Y + prefix * r;
  177. }
  178. else
  179. {
  180. //
  181. // Use the following form:
  182. //
  183. // lgamma(z) = (2-z)(1-z)(Y + R(2-z))
  184. //
  185. // where R(2-z) is a rational approximation optimised for
  186. // low absolute error - as long as it's absolute error
  187. // is small compared to the constant Y - then any rounding
  188. // error in it's computation will get wiped out.
  189. //
  190. // R(2-z) has the following properties:
  191. //
  192. // At double precision, max error found: 1.797565e-17
  193. // At 80-bit long double precision, max error found: 9.306419e-21
  194. // Maximum Deviation Found: 2.151e-021
  195. // Expected Error Term: 2.150e-021
  196. //
  197. // LCOV_EXCL_START
  198. constexpr float Y = 0.452017307281494140625f;
  199. BOOST_MATH_STATIC const T P[] = {
  200. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.292329721830270012337e-1)),
  201. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.144216267757192309184e0)),
  202. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.142440390738631274135e0)),
  203. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.542809694055053558157e-1)),
  204. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.850535976868336437746e-2)),
  205. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.431171342679297331241e-3))
  206. };
  207. BOOST_MATH_STATIC const T Q[] = {
  208. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.1e1)),
  209. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.150169356054485044494e1)),
  210. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.846973248876495016101e0)),
  211. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.220095151814995745555e0)),
  212. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.25582797155975869989e-1)),
  213. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.100666795539143372762e-2)),
  214. static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -0.827193521891290553639e-6))
  215. };
  216. // LCOV_EXCL_STOP
  217. T r = zm2 * zm1;
  218. T R = tools::evaluate_polynomial(P, T(-zm2)) / tools::evaluate_polynomial(Q, T(-zm2));
  219. result += r * Y + r * R;
  220. }
  221. }
  222. return result;
  223. }
  224. #ifndef BOOST_MATH_HAS_GPU_SUPPORT
  225. //
  226. // 128-bit floats aren't directly tested in our coverage tests (takes too long)
  227. // LCOV_EXCL_START
  228. //
  229. template <class T, class Policy, class Lanczos>
  230. T lgamma_small_imp(T z, T zm1, T zm2, const boost::math::integral_constant<int, 113>&, const Policy& /* l */, const Lanczos&)
  231. {
  232. //
  233. // This version uses rational approximations for small
  234. // values of z accurate enough for 113-bit mantissas
  235. // (128-bit long doubles).
  236. //
  237. BOOST_MATH_STD_USING // for ADL of std names
  238. T result = 0;
  239. BOOST_MATH_ASSERT(z >= tools::root_epsilon<T>());
  240. /*
  241. * Can not be reached:
  242. if(z < tools::epsilon<T>())
  243. {
  244. result = -log(z);
  245. BOOST_MATH_INSTRUMENT_CODE(result);
  246. }
  247. */
  248. if((zm1 == 0) || (zm2 == 0))
  249. {
  250. // nothing to do, result is zero....
  251. }
  252. else if(z > 2)
  253. {
  254. //
  255. // Begin by performing argument reduction until
  256. // z is in [2,3):
  257. //
  258. if(z >= 3)
  259. {
  260. do
  261. {
  262. z -= 1;
  263. result += log(z);
  264. }while(z >= 3);
  265. zm2 = z - 2;
  266. }
  267. BOOST_MATH_INSTRUMENT_CODE(zm2);
  268. BOOST_MATH_INSTRUMENT_CODE(z);
  269. BOOST_MATH_INSTRUMENT_CODE(result);
  270. //
  271. // Use the following form:
  272. //
  273. // lgamma(z) = (z-2)(z+1)(Y + R(z-2))
  274. //
  275. // where R(z-2) is a rational approximation optimised for
  276. // low absolute error - as long as it's absolute error
  277. // is small compared to the constant Y - then any rounding
  278. // error in it's computation will get wiped out.
  279. //
  280. // Maximum Deviation Found (approximation error) 3.73e-37
  281. static const T P[] = {
  282. BOOST_MATH_BIG_CONSTANT(T, 113, -0.018035568567844937910504030027467476655),
  283. BOOST_MATH_BIG_CONSTANT(T, 113, 0.013841458273109517271750705401202404195),
  284. BOOST_MATH_BIG_CONSTANT(T, 113, 0.062031842739486600078866923383017722399),
  285. BOOST_MATH_BIG_CONSTANT(T, 113, 0.052518418329052161202007865149435256093),
  286. BOOST_MATH_BIG_CONSTANT(T, 113, 0.01881718142472784129191838493267755758),
  287. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0025104830367021839316463675028524702846),
  288. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00021043176101831873281848891452678568311),
  289. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00010249622350908722793327719494037981166),
  290. BOOST_MATH_BIG_CONSTANT(T, 113, -0.11381479670982006841716879074288176994e-4),
  291. BOOST_MATH_BIG_CONSTANT(T, 113, -0.49999811718089980992888533630523892389e-6),
  292. BOOST_MATH_BIG_CONSTANT(T, 113, -0.70529798686542184668416911331718963364e-8)
  293. };
  294. static const T Q[] = {
  295. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  296. BOOST_MATH_BIG_CONSTANT(T, 113, 2.5877485070422317542808137697939233685),
  297. BOOST_MATH_BIG_CONSTANT(T, 113, 2.8797959228352591788629602533153837126),
  298. BOOST_MATH_BIG_CONSTANT(T, 113, 1.8030885955284082026405495275461180977),
  299. BOOST_MATH_BIG_CONSTANT(T, 113, 0.69774331297747390169238306148355428436),
  300. BOOST_MATH_BIG_CONSTANT(T, 113, 0.17261566063277623942044077039756583802),
  301. BOOST_MATH_BIG_CONSTANT(T, 113, 0.02729301254544230229429621192443000121),
  302. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0026776425891195270663133581960016620433),
  303. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00015244249160486584591370355730402168106),
  304. BOOST_MATH_BIG_CONSTANT(T, 113, 0.43997034032479866020546814475414346627e-5),
  305. BOOST_MATH_BIG_CONSTANT(T, 113, 0.46295080708455613044541885534408170934e-7),
  306. BOOST_MATH_BIG_CONSTANT(T, 113, -0.93326638207459533682980757982834180952e-11),
  307. BOOST_MATH_BIG_CONSTANT(T, 113, 0.42316456553164995177177407325292867513e-13)
  308. };
  309. T R = tools::evaluate_polynomial(P, zm2);
  310. R /= tools::evaluate_polynomial(Q, zm2);
  311. static const float Y = 0.158963680267333984375F;
  312. T r = zm2 * (z + 1);
  313. result += r * Y + r * R;
  314. BOOST_MATH_INSTRUMENT_CODE(result);
  315. }
  316. else
  317. {
  318. //
  319. // If z is less than 1 use recurrence to shift to
  320. // z in the interval [1,2]:
  321. //
  322. if(z < 1)
  323. {
  324. result += -log(z);
  325. zm2 = zm1;
  326. zm1 = z;
  327. z += 1;
  328. }
  329. BOOST_MATH_INSTRUMENT_CODE(result);
  330. BOOST_MATH_INSTRUMENT_CODE(z);
  331. BOOST_MATH_INSTRUMENT_CODE(zm2);
  332. //
  333. // Three approximations, on for z in [1,1.35], [1.35,1.625] and [1.625,1]
  334. //
  335. if(z <= 1.35)
  336. {
  337. //
  338. // Use the following form:
  339. //
  340. // lgamma(z) = (z-1)(z-2)(Y + R(z-1))
  341. //
  342. // where R(z-1) is a rational approximation optimised for
  343. // low absolute error - as long as it's absolute error
  344. // is small compared to the constant Y - then any rounding
  345. // error in it's computation will get wiped out.
  346. //
  347. // R(z-1) has the following properties:
  348. //
  349. // Maximum Deviation Found (approximation error) 1.659e-36
  350. // Expected Error Term (theoretical error) 1.343e-36
  351. // Max error found at 128-bit long double precision 1.007e-35
  352. //
  353. static const float Y = 0.54076099395751953125f;
  354. static const T P[] = {
  355. BOOST_MATH_BIG_CONSTANT(T, 113, 0.036454670944013329356512090082402429697),
  356. BOOST_MATH_BIG_CONSTANT(T, 113, -0.066235835556476033710068679907798799959),
  357. BOOST_MATH_BIG_CONSTANT(T, 113, -0.67492399795577182387312206593595565371),
  358. BOOST_MATH_BIG_CONSTANT(T, 113, -1.4345555263962411429855341651960000166),
  359. BOOST_MATH_BIG_CONSTANT(T, 113, -1.4894319559821365820516771951249649563),
  360. BOOST_MATH_BIG_CONSTANT(T, 113, -0.87210277668067964629483299712322411566),
  361. BOOST_MATH_BIG_CONSTANT(T, 113, -0.29602090537771744401524080430529369136),
  362. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0561832587517836908929331992218879676),
  363. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0053236785487328044334381502530383140443),
  364. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00018629360291358130461736386077971890789),
  365. BOOST_MATH_BIG_CONSTANT(T, 113, -0.10164985672213178500790406939467614498e-6),
  366. BOOST_MATH_BIG_CONSTANT(T, 113, 0.13680157145361387405588201461036338274e-8)
  367. };
  368. static const T Q[] = {
  369. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  370. BOOST_MATH_BIG_CONSTANT(T, 113, 4.9106336261005990534095838574132225599),
  371. BOOST_MATH_BIG_CONSTANT(T, 113, 10.258804800866438510889341082793078432),
  372. BOOST_MATH_BIG_CONSTANT(T, 113, 11.88588976846826108836629960537466889),
  373. BOOST_MATH_BIG_CONSTANT(T, 113, 8.3455000546999704314454891036700998428),
  374. BOOST_MATH_BIG_CONSTANT(T, 113, 3.6428823682421746343233362007194282703),
  375. BOOST_MATH_BIG_CONSTANT(T, 113, 0.97465989807254572142266753052776132252),
  376. BOOST_MATH_BIG_CONSTANT(T, 113, 0.15121052897097822172763084966793352524),
  377. BOOST_MATH_BIG_CONSTANT(T, 113, 0.012017363555383555123769849654484594893),
  378. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0003583032812720649835431669893011257277)
  379. };
  380. T r = tools::evaluate_polynomial(P, zm1) / tools::evaluate_polynomial(Q, zm1);
  381. T prefix = zm1 * zm2;
  382. result += prefix * Y + prefix * r;
  383. BOOST_MATH_INSTRUMENT_CODE(result);
  384. }
  385. else if(z <= 1.625)
  386. {
  387. //
  388. // Use the following form:
  389. //
  390. // lgamma(z) = (2-z)(1-z)(Y + R(2-z))
  391. //
  392. // where R(2-z) is a rational approximation optimised for
  393. // low absolute error - as long as it's absolute error
  394. // is small compared to the constant Y - then any rounding
  395. // error in it's computation will get wiped out.
  396. //
  397. // R(2-z) has the following properties:
  398. //
  399. // Max error found at 128-bit long double precision 9.634e-36
  400. // Maximum Deviation Found (approximation error) 1.538e-37
  401. // Expected Error Term (theoretical error) 2.350e-38
  402. //
  403. static const float Y = 0.483787059783935546875f;
  404. static const T P[] = {
  405. BOOST_MATH_BIG_CONSTANT(T, 113, -0.017977422421608624353488126610933005432),
  406. BOOST_MATH_BIG_CONSTANT(T, 113, 0.18484528905298309555089509029244135703),
  407. BOOST_MATH_BIG_CONSTANT(T, 113, -0.40401251514859546989565001431430884082),
  408. BOOST_MATH_BIG_CONSTANT(T, 113, 0.40277179799147356461954182877921388182),
  409. BOOST_MATH_BIG_CONSTANT(T, 113, -0.21993421441282936476709677700477598816),
  410. BOOST_MATH_BIG_CONSTANT(T, 113, 0.069595742223850248095697771331107571011),
  411. BOOST_MATH_BIG_CONSTANT(T, 113, -0.012681481427699686635516772923547347328),
  412. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0012489322866834830413292771335113136034),
  413. BOOST_MATH_BIG_CONSTANT(T, 113, -0.57058739515423112045108068834668269608e-4),
  414. BOOST_MATH_BIG_CONSTANT(T, 113, 0.8207548771933585614380644961342925976e-6)
  415. };
  416. static const T Q[] = {
  417. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  418. BOOST_MATH_BIG_CONSTANT(T, 113, -2.9629552288944259229543137757200262073),
  419. BOOST_MATH_BIG_CONSTANT(T, 113, 3.7118380799042118987185957298964772755),
  420. BOOST_MATH_BIG_CONSTANT(T, 113, -2.5569815272165399297600586376727357187),
  421. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0546764918220835097855665680632153367),
  422. BOOST_MATH_BIG_CONSTANT(T, 113, -0.26574021300894401276478730940980810831),
  423. BOOST_MATH_BIG_CONSTANT(T, 113, 0.03996289731752081380552901986471233462),
  424. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0033398680924544836817826046380586480873),
  425. BOOST_MATH_BIG_CONSTANT(T, 113, 0.00013288854760548251757651556792598235735),
  426. BOOST_MATH_BIG_CONSTANT(T, 113, -0.17194794958274081373243161848194745111e-5)
  427. };
  428. T r = zm2 * zm1;
  429. T R = tools::evaluate_polynomial(P, T(0.625 - zm1)) / tools::evaluate_polynomial(Q, T(0.625 - zm1));
  430. result += r * Y + r * R;
  431. BOOST_MATH_INSTRUMENT_CODE(result);
  432. }
  433. else
  434. {
  435. //
  436. // Same form as above.
  437. //
  438. // Max error found (at 128-bit long double precision) 1.831e-35
  439. // Maximum Deviation Found (approximation error) 8.588e-36
  440. // Expected Error Term (theoretical error) 1.458e-36
  441. //
  442. static const float Y = 0.443811893463134765625f;
  443. static const T P[] = {
  444. BOOST_MATH_BIG_CONSTANT(T, 113, -0.021027558364667626231512090082402429494),
  445. BOOST_MATH_BIG_CONSTANT(T, 113, 0.15128811104498736604523586803722368377),
  446. BOOST_MATH_BIG_CONSTANT(T, 113, -0.26249631480066246699388544451126410278),
  447. BOOST_MATH_BIG_CONSTANT(T, 113, 0.21148748610533489823742352180628489742),
  448. BOOST_MATH_BIG_CONSTANT(T, 113, -0.093964130697489071999873506148104370633),
  449. BOOST_MATH_BIG_CONSTANT(T, 113, 0.024292059227009051652542804957550866827),
  450. BOOST_MATH_BIG_CONSTANT(T, 113, -0.0036284453226534839926304745756906117066),
  451. BOOST_MATH_BIG_CONSTANT(T, 113, 0.0002939230129315195346843036254392485984),
  452. BOOST_MATH_BIG_CONSTANT(T, 113, -0.11088589183158123733132268042570710338e-4),
  453. BOOST_MATH_BIG_CONSTANT(T, 113, 0.13240510580220763969511741896361984162e-6)
  454. };
  455. static const T Q[] = {
  456. BOOST_MATH_BIG_CONSTANT(T, 113, 1.0),
  457. BOOST_MATH_BIG_CONSTANT(T, 113, -2.4240003754444040525462170802796471996),
  458. BOOST_MATH_BIG_CONSTANT(T, 113, 2.4868383476933178722203278602342786002),
  459. BOOST_MATH_BIG_CONSTANT(T, 113, -1.4047068395206343375520721509193698547),
  460. BOOST_MATH_BIG_CONSTANT(T, 113, 0.47583809087867443858344765659065773369),
  461. BOOST_MATH_BIG_CONSTANT(T, 113, -0.09865724264554556400463655444270700132),
  462. BOOST_MATH_BIG_CONSTANT(T, 113, 0.012238223514176587501074150988445109735),
  463. BOOST_MATH_BIG_CONSTANT(T, 113, -0.00084625068418239194670614419707491797097),
  464. BOOST_MATH_BIG_CONSTANT(T, 113, 0.2796574430456237061420839429225710602e-4),
  465. BOOST_MATH_BIG_CONSTANT(T, 113, -0.30202973883316730694433702165188835331e-6)
  466. };
  467. // (2 - x) * (1 - x) * (c + R(2 - x))
  468. T r = zm2 * zm1;
  469. T R = tools::evaluate_polynomial(P, T(-zm2)) / tools::evaluate_polynomial(Q, T(-zm2));
  470. result += r * Y + r * R;
  471. BOOST_MATH_INSTRUMENT_CODE(result);
  472. }
  473. }
  474. BOOST_MATH_INSTRUMENT_CODE(result);
  475. return result;
  476. }
  477. // LCOV_EXCL_STOP
  478. template <class T, class Policy, class Lanczos>
  479. BOOST_MATH_GPU_ENABLED T lgamma_small_imp(T z, T zm1, T zm2, const boost::math::integral_constant<int, 0>&, const Policy& pol, const Lanczos& l)
  480. {
  481. //
  482. // No rational approximations are available because either
  483. // T has no numeric_limits support (so we can't tell how
  484. // many digits it has), or T has more digits than we know
  485. // what to do with.... we do have a Lanczos approximation
  486. // though, and that can be used to keep errors under control.
  487. //
  488. BOOST_MATH_STD_USING // for ADL of std names
  489. T result = 0;
  490. BOOST_MATH_ASSERT(z >= tools::root_epsilon<T>());
  491. /*
  492. * Not reachable:
  493. if(z < tools::epsilon<T>())
  494. {
  495. result = -log(z);
  496. }
  497. */
  498. if(z < 0.5)
  499. {
  500. // taking the log of tgamma reduces the error, no danger of overflow here:
  501. result = log(gamma_imp(z, pol, Lanczos()));
  502. }
  503. else if(z >= 3)
  504. {
  505. // taking the log of tgamma reduces the error, no danger of overflow here:
  506. result = log(gamma_imp(z, pol, Lanczos()));
  507. }
  508. else if(z >= 1.5)
  509. {
  510. // special case near 2:
  511. T dz = zm2;
  512. result = dz * log((z + lanczos_g_near_1_and_2(l) - T(0.5)) / boost::math::constants::e<T>());
  513. result += boost::math::log1p(dz / (lanczos_g_near_1_and_2(l) + T(1.5)), pol) * T(1.5);
  514. result += boost::math::log1p(Lanczos::lanczos_sum_near_2(dz), pol);
  515. }
  516. else
  517. {
  518. // special case near 1:
  519. T dz = zm1;
  520. result = dz * log((z + lanczos_g_near_1_and_2(l) - T(0.5)) / boost::math::constants::e<T>());
  521. result += boost::math::log1p(dz / (lanczos_g_near_1_and_2(l) + T(0.5)), pol) / 2;
  522. result += boost::math::log1p(Lanczos::lanczos_sum_near_1(dz), pol);
  523. }
  524. return result;
  525. }
  526. #endif // BOOST_MATH_HAS_GPU_SUPPORT
  527. }}} // namespaces
  528. #endif // BOOST_MATH_SPECIAL_FUNCTIONS_DETAIL_LGAMMA_SMALL