lgamma_small.hpp 23 KB

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