lgamma_small.hpp 22 KB

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