calculate_constants.hpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. // Copyright John Maddock 2010, 2012.
  2. // Copyright Paul A. Bristow 2011, 2012.
  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_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
  7. #define BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
  8. #include <boost/static_assert.hpp>
  9. #include <type_traits>
  10. namespace boost{ namespace math{ namespace constants{ namespace detail{
  11. template <class T>
  12. template<int N>
  13. inline T constant_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  14. {
  15. BOOST_MATH_STD_USING
  16. return ldexp(acos(T(0)), 1);
  17. /*
  18. // Although this code works well, it's usually more accurate to just call acos
  19. // and access the number types own representation of PI which is usually calculated
  20. // at slightly higher precision...
  21. T result;
  22. T a = 1;
  23. T b;
  24. T A(a);
  25. T B = 0.5f;
  26. T D = 0.25f;
  27. T lim;
  28. lim = boost::math::tools::epsilon<T>();
  29. unsigned k = 1;
  30. do
  31. {
  32. result = A + B;
  33. result = ldexp(result, -2);
  34. b = sqrt(B);
  35. a += b;
  36. a = ldexp(a, -1);
  37. A = a * a;
  38. B = A - result;
  39. B = ldexp(B, 1);
  40. result = A - B;
  41. bool neg = boost::math::sign(result) < 0;
  42. if(neg)
  43. result = -result;
  44. if(result <= lim)
  45. break;
  46. if(neg)
  47. result = -result;
  48. result = ldexp(result, k - 1);
  49. D -= result;
  50. ++k;
  51. lim = ldexp(lim, 1);
  52. }
  53. while(true);
  54. result = B / D;
  55. return result;
  56. */
  57. }
  58. template <class T>
  59. template<int N>
  60. inline T constant_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  61. {
  62. return 2 * pi<T, policies::policy<policies::digits2<N> > >();
  63. }
  64. template <class T> // 2 / pi
  65. template<int N>
  66. inline T constant_two_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  67. {
  68. return 2 / pi<T, policies::policy<policies::digits2<N> > >();
  69. }
  70. template <class T> // sqrt(2/pi)
  71. template <int N>
  72. inline T constant_root_two_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  73. {
  74. BOOST_MATH_STD_USING
  75. return sqrt((2 / pi<T, policies::policy<policies::digits2<N> > >()));
  76. }
  77. template <class T>
  78. template<int N>
  79. inline T constant_one_div_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  80. {
  81. return 1 / two_pi<T, policies::policy<policies::digits2<N> > >();
  82. }
  83. template <class T>
  84. template<int N>
  85. inline T constant_root_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  86. {
  87. BOOST_MATH_STD_USING
  88. return sqrt(pi<T, policies::policy<policies::digits2<N> > >());
  89. }
  90. template <class T>
  91. template<int N>
  92. inline T constant_root_half_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  93. {
  94. BOOST_MATH_STD_USING
  95. return sqrt(pi<T, policies::policy<policies::digits2<N> > >() / 2);
  96. }
  97. template <class T>
  98. template<int N>
  99. inline T constant_root_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  100. {
  101. BOOST_MATH_STD_USING
  102. return sqrt(two_pi<T, policies::policy<policies::digits2<N> > >());
  103. }
  104. template <class T>
  105. template<int N>
  106. inline T constant_log_root_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  107. {
  108. BOOST_MATH_STD_USING
  109. return log(root_two_pi<T, policies::policy<policies::digits2<N> > >());
  110. }
  111. template <class T>
  112. template<int N>
  113. inline T constant_root_ln_four<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  114. {
  115. BOOST_MATH_STD_USING
  116. return sqrt(log(static_cast<T>(4)));
  117. }
  118. template <class T>
  119. template<int N>
  120. inline T constant_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  121. {
  122. //
  123. // Although we can clearly calculate this from first principles, this hooks into
  124. // T's own notion of e, which hopefully will more accurate than one calculated to
  125. // a few epsilon:
  126. //
  127. BOOST_MATH_STD_USING
  128. return exp(static_cast<T>(1));
  129. }
  130. template <class T>
  131. template<int N>
  132. inline T constant_half<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  133. {
  134. return static_cast<T>(1) / static_cast<T>(2);
  135. }
  136. template <class T>
  137. template<int M>
  138. inline T constant_euler<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, M>)))
  139. {
  140. BOOST_MATH_STD_USING
  141. //
  142. // This is the method described in:
  143. // "Some New Algorithms for High-Precision Computation of Euler's Constant"
  144. // Richard P Brent and Edwin M McMillan.
  145. // Mathematics of Computation, Volume 34, Number 149, Jan 1980, pages 305-312.
  146. // See equation 17 with p = 2.
  147. //
  148. T n = 3 + (M ? (std::min)(M, tools::digits<T>()) : tools::digits<T>()) / 4;
  149. T lim = M ? ldexp(T(1), 1 - (std::min)(M, tools::digits<T>())) : tools::epsilon<T>();
  150. T lnn = log(n);
  151. T term = 1;
  152. T N = -lnn;
  153. T D = 1;
  154. T Hk = 0;
  155. T one = 1;
  156. for(unsigned k = 1;; ++k)
  157. {
  158. term *= n * n;
  159. term /= k * k;
  160. Hk += one / k;
  161. N += term * (Hk - lnn);
  162. D += term;
  163. if(term < D * lim)
  164. break;
  165. }
  166. return N / D;
  167. }
  168. template <class T>
  169. template<int N>
  170. inline T constant_euler_sqr<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  171. {
  172. BOOST_MATH_STD_USING
  173. return euler<T, policies::policy<policies::digits2<N> > >()
  174. * euler<T, policies::policy<policies::digits2<N> > >();
  175. }
  176. template <class T>
  177. template<int N>
  178. inline T constant_one_div_euler<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  179. {
  180. BOOST_MATH_STD_USING
  181. return static_cast<T>(1)
  182. / euler<T, policies::policy<policies::digits2<N> > >();
  183. }
  184. template <class T>
  185. template<int N>
  186. inline T constant_root_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  187. {
  188. BOOST_MATH_STD_USING
  189. return sqrt(static_cast<T>(2));
  190. }
  191. template <class T>
  192. template<int N>
  193. inline T constant_root_three<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  194. {
  195. BOOST_MATH_STD_USING
  196. return sqrt(static_cast<T>(3));
  197. }
  198. template <class T>
  199. template<int N>
  200. inline T constant_half_root_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  201. {
  202. BOOST_MATH_STD_USING
  203. return sqrt(static_cast<T>(2)) / 2;
  204. }
  205. template <class T>
  206. template<int N>
  207. inline T constant_ln_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  208. {
  209. //
  210. // Although there are good ways to calculate this from scratch, this hooks into
  211. // T's own notion of log(2) which will hopefully be accurate to the full precision
  212. // of T:
  213. //
  214. BOOST_MATH_STD_USING
  215. return log(static_cast<T>(2));
  216. }
  217. template <class T>
  218. template<int N>
  219. inline T constant_ln_ten<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  220. {
  221. BOOST_MATH_STD_USING
  222. return log(static_cast<T>(10));
  223. }
  224. template <class T>
  225. template<int N>
  226. inline T constant_ln_ln_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  227. {
  228. BOOST_MATH_STD_USING
  229. return log(log(static_cast<T>(2)));
  230. }
  231. template <class T>
  232. template<int N>
  233. inline T constant_third<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  234. {
  235. BOOST_MATH_STD_USING
  236. return static_cast<T>(1) / static_cast<T>(3);
  237. }
  238. template <class T>
  239. template<int N>
  240. inline T constant_twothirds<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  241. {
  242. BOOST_MATH_STD_USING
  243. return static_cast<T>(2) / static_cast<T>(3);
  244. }
  245. template <class T>
  246. template<int N>
  247. inline T constant_two_thirds<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  248. {
  249. BOOST_MATH_STD_USING
  250. return static_cast<T>(2) / static_cast<T>(3);
  251. }
  252. template <class T>
  253. template<int N>
  254. inline T constant_three_quarters<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  255. {
  256. BOOST_MATH_STD_USING
  257. return static_cast<T>(3) / static_cast<T>(4);
  258. }
  259. template <class T>
  260. template<int N>
  261. inline T constant_sixth<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  262. {
  263. BOOST_MATH_STD_USING
  264. return static_cast<T>(1) / static_cast<T>(6);
  265. }
  266. // Pi and related constants.
  267. template <class T>
  268. template<int N>
  269. inline T constant_pi_minus_three<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  270. {
  271. return pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(3);
  272. }
  273. template <class T>
  274. template<int N>
  275. inline T constant_four_minus_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  276. {
  277. return static_cast<T>(4) - pi<T, policies::policy<policies::digits2<N> > >();
  278. }
  279. template <class T>
  280. template<int N>
  281. inline T constant_exp_minus_half<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  282. {
  283. BOOST_MATH_STD_USING
  284. return exp(static_cast<T>(-0.5));
  285. }
  286. template <class T>
  287. template<int N>
  288. inline T constant_exp_minus_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  289. {
  290. BOOST_MATH_STD_USING
  291. return exp(static_cast<T>(-1.));
  292. }
  293. template <class T>
  294. template<int N>
  295. inline T constant_one_div_root_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  296. {
  297. return static_cast<T>(1) / root_two<T, policies::policy<policies::digits2<N> > >();
  298. }
  299. template <class T>
  300. template<int N>
  301. inline T constant_one_div_root_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  302. {
  303. return static_cast<T>(1) / root_pi<T, policies::policy<policies::digits2<N> > >();
  304. }
  305. template <class T>
  306. template<int N>
  307. inline T constant_one_div_root_two_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  308. {
  309. return static_cast<T>(1) / root_two_pi<T, policies::policy<policies::digits2<N> > >();
  310. }
  311. template <class T>
  312. template<int N>
  313. inline T constant_root_one_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  314. {
  315. BOOST_MATH_STD_USING
  316. return sqrt(static_cast<T>(1) / pi<T, policies::policy<policies::digits2<N> > >());
  317. }
  318. template <class T>
  319. template<int N>
  320. inline T constant_four_thirds_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  321. {
  322. BOOST_MATH_STD_USING
  323. return pi<T, policies::policy<policies::digits2<N> > >() * static_cast<T>(4) / static_cast<T>(3);
  324. }
  325. template <class T>
  326. template<int N>
  327. inline T constant_half_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  328. {
  329. BOOST_MATH_STD_USING
  330. return pi<T, policies::policy<policies::digits2<N> > >() / static_cast<T>(2);
  331. }
  332. template <class T>
  333. template<int N>
  334. inline T constant_third_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  335. {
  336. BOOST_MATH_STD_USING
  337. return pi<T, policies::policy<policies::digits2<N> > >() / static_cast<T>(3);
  338. }
  339. template <class T>
  340. template<int N>
  341. inline T constant_sixth_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  342. {
  343. BOOST_MATH_STD_USING
  344. return pi<T, policies::policy<policies::digits2<N> > >() / static_cast<T>(6);
  345. }
  346. template <class T>
  347. template<int N>
  348. inline T constant_two_thirds_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  349. {
  350. BOOST_MATH_STD_USING
  351. return pi<T, policies::policy<policies::digits2<N> > >() * static_cast<T>(2) / static_cast<T>(3);
  352. }
  353. template <class T>
  354. template<int N>
  355. inline T constant_three_quarters_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  356. {
  357. BOOST_MATH_STD_USING
  358. return pi<T, policies::policy<policies::digits2<N> > >() * static_cast<T>(3) / static_cast<T>(4);
  359. }
  360. template <class T>
  361. template<int N>
  362. inline T constant_pi_pow_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  363. {
  364. BOOST_MATH_STD_USING
  365. return pow(pi<T, policies::policy<policies::digits2<N> > >(), e<T, policies::policy<policies::digits2<N> > >()); //
  366. }
  367. template <class T>
  368. template<int N>
  369. inline T constant_pi_sqr<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  370. {
  371. BOOST_MATH_STD_USING
  372. return pi<T, policies::policy<policies::digits2<N> > >()
  373. * pi<T, policies::policy<policies::digits2<N> > >() ; //
  374. }
  375. template <class T>
  376. template<int N>
  377. inline T constant_pi_sqr_div_six<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  378. {
  379. BOOST_MATH_STD_USING
  380. return pi<T, policies::policy<policies::digits2<N> > >()
  381. * pi<T, policies::policy<policies::digits2<N> > >()
  382. / static_cast<T>(6); //
  383. }
  384. template <class T>
  385. template<int N>
  386. inline T constant_pi_cubed<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  387. {
  388. BOOST_MATH_STD_USING
  389. return pi<T, policies::policy<policies::digits2<N> > >()
  390. * pi<T, policies::policy<policies::digits2<N> > >()
  391. * pi<T, policies::policy<policies::digits2<N> > >()
  392. ; //
  393. }
  394. template <class T>
  395. template<int N>
  396. inline T constant_cbrt_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  397. {
  398. BOOST_MATH_STD_USING
  399. return pow(pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1)/ static_cast<T>(3));
  400. }
  401. template <class T>
  402. template<int N>
  403. inline T constant_one_div_cbrt_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  404. {
  405. BOOST_MATH_STD_USING
  406. return static_cast<T>(1)
  407. / pow(pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(1)/ static_cast<T>(3));
  408. }
  409. // Euler's e
  410. template <class T>
  411. template<int N>
  412. inline T constant_e_pow_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  413. {
  414. BOOST_MATH_STD_USING
  415. return pow(e<T, policies::policy<policies::digits2<N> > >(), pi<T, policies::policy<policies::digits2<N> > >()); //
  416. }
  417. template <class T>
  418. template<int N>
  419. inline T constant_root_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  420. {
  421. BOOST_MATH_STD_USING
  422. return sqrt(e<T, policies::policy<policies::digits2<N> > >());
  423. }
  424. template <class T>
  425. template<int N>
  426. inline T constant_log10_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  427. {
  428. BOOST_MATH_STD_USING
  429. return log10(e<T, policies::policy<policies::digits2<N> > >());
  430. }
  431. template <class T>
  432. template<int N>
  433. inline T constant_one_div_log10_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  434. {
  435. BOOST_MATH_STD_USING
  436. return static_cast<T>(1) /
  437. log10(e<T, policies::policy<policies::digits2<N> > >());
  438. }
  439. // Trigonometric
  440. template <class T>
  441. template<int N>
  442. inline T constant_degree<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  443. {
  444. BOOST_MATH_STD_USING
  445. return pi<T, policies::policy<policies::digits2<N> > >()
  446. / static_cast<T>(180)
  447. ; //
  448. }
  449. template <class T>
  450. template<int N>
  451. inline T constant_radian<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  452. {
  453. BOOST_MATH_STD_USING
  454. return static_cast<T>(180)
  455. / pi<T, policies::policy<policies::digits2<N> > >()
  456. ; //
  457. }
  458. template <class T>
  459. template<int N>
  460. inline T constant_sin_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  461. {
  462. BOOST_MATH_STD_USING
  463. return sin(static_cast<T>(1)) ; //
  464. }
  465. template <class T>
  466. template<int N>
  467. inline T constant_cos_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  468. {
  469. BOOST_MATH_STD_USING
  470. return cos(static_cast<T>(1)) ; //
  471. }
  472. template <class T>
  473. template<int N>
  474. inline T constant_sinh_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  475. {
  476. BOOST_MATH_STD_USING
  477. return sinh(static_cast<T>(1)) ; //
  478. }
  479. template <class T>
  480. template<int N>
  481. inline T constant_cosh_one<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  482. {
  483. BOOST_MATH_STD_USING
  484. return cosh(static_cast<T>(1)) ; //
  485. }
  486. template <class T>
  487. template<int N>
  488. inline T constant_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  489. {
  490. BOOST_MATH_STD_USING
  491. return (static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) ; //
  492. }
  493. template <class T>
  494. template<int N>
  495. inline T constant_ln_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  496. {
  497. BOOST_MATH_STD_USING
  498. return log((static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) );
  499. }
  500. template <class T>
  501. template<int N>
  502. inline T constant_one_div_ln_phi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  503. {
  504. BOOST_MATH_STD_USING
  505. return static_cast<T>(1) /
  506. log((static_cast<T>(1) + sqrt(static_cast<T>(5)) )/static_cast<T>(2) );
  507. }
  508. // Zeta
  509. template <class T>
  510. template<int N>
  511. inline T constant_zeta_two<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  512. {
  513. BOOST_MATH_STD_USING
  514. return pi<T, policies::policy<policies::digits2<N> > >()
  515. * pi<T, policies::policy<policies::digits2<N> > >()
  516. /static_cast<T>(6);
  517. }
  518. template <class T>
  519. template<int N>
  520. inline T constant_zeta_three<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  521. {
  522. // http://mathworld.wolfram.com/AperysConstant.html
  523. // http://en.wikipedia.org/wiki/Mathematical_constant
  524. // http://oeis.org/A002117/constant
  525. //T zeta3("1.20205690315959428539973816151144999076"
  526. // "4986292340498881792271555341838205786313"
  527. // "09018645587360933525814619915");
  528. //"1.202056903159594285399738161511449990, 76498629234049888179227155534183820578631309018645587360933525814619915" A002117
  529. // 1.202056903159594285399738161511449990, 76498629234049888179227155534183820578631309018645587360933525814619915780, +00);
  530. //"1.2020569031595942 double
  531. // http://www.spaennare.se/SSPROG/ssnum.pdf // section 11, Algorithm for Apery's constant zeta(3).
  532. // Programs to Calculate some Mathematical Constants to Large Precision, Document Version 1.50
  533. // by Stefan Spannare September 19, 2007
  534. // zeta(3) = 1/64 * sum
  535. BOOST_MATH_STD_USING
  536. T n_fact=static_cast<T>(1); // build n! for n = 0.
  537. T sum = static_cast<double>(77); // Start with n = 0 case.
  538. // for n = 0, (77/1) /64 = 1.203125
  539. //double lim = std::numeric_limits<double>::epsilon();
  540. T lim = N ? ldexp(T(1), 1 - (std::min)(N, tools::digits<T>())) : tools::epsilon<T>();
  541. for(unsigned int n = 1; n < 40; ++n)
  542. { // three to five decimal digits per term, so 40 should be plenty for 100 decimal digits.
  543. //cout << "n = " << n << endl;
  544. n_fact *= n; // n!
  545. T n_fact_p10 = n_fact * n_fact * n_fact * n_fact * n_fact * n_fact * n_fact * n_fact * n_fact * n_fact; // (n!)^10
  546. T num = ((205 * n * n) + (250 * n) + 77) * n_fact_p10; // 205n^2 + 250n + 77
  547. // int nn = (2 * n + 1);
  548. // T d = factorial(nn); // inline factorial.
  549. T d = 1;
  550. for(unsigned int i = 1; i <= (n+n + 1); ++i) // (2n + 1)
  551. {
  552. d *= i;
  553. }
  554. T den = d * d * d * d * d; // [(2n+1)!]^5
  555. //cout << "den = " << den << endl;
  556. T term = num/den;
  557. if (n % 2 != 0)
  558. { //term *= -1;
  559. sum -= term;
  560. }
  561. else
  562. {
  563. sum += term;
  564. }
  565. //cout << "term = " << term << endl;
  566. //cout << "sum/64 = " << sum/64 << endl;
  567. if(abs(term) < lim)
  568. {
  569. break;
  570. }
  571. }
  572. return sum / 64;
  573. }
  574. template <class T>
  575. template<int N>
  576. inline T constant_catalan<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  577. { // http://oeis.org/A006752/constant
  578. //T c("0.915965594177219015054603514932384110774"
  579. //"149374281672134266498119621763019776254769479356512926115106248574");
  580. // 9.159655941772190150546035149323841107, 74149374281672134266498119621763019776254769479356512926115106248574422619, -01);
  581. // This is equation (entry) 31 from
  582. // http://www-2.cs.cmu.edu/~adamchik/articles/catalan/catalan.htm
  583. // See also http://www.mpfr.org/algorithms.pdf
  584. BOOST_MATH_STD_USING
  585. T k_fact = 1;
  586. T tk_fact = 1;
  587. T sum = 1;
  588. T term;
  589. T lim = N ? ldexp(T(1), 1 - (std::min)(N, tools::digits<T>())) : tools::epsilon<T>();
  590. for(unsigned k = 1;; ++k)
  591. {
  592. k_fact *= k;
  593. tk_fact *= (2 * k) * (2 * k - 1);
  594. term = k_fact * k_fact / (tk_fact * (2 * k + 1) * (2 * k + 1));
  595. sum += term;
  596. if(term < lim)
  597. {
  598. break;
  599. }
  600. }
  601. return boost::math::constants::pi<T, boost::math::policies::policy<> >()
  602. * log(2 + boost::math::constants::root_three<T, boost::math::policies::policy<> >())
  603. / 8
  604. + 3 * sum / 8;
  605. }
  606. namespace khinchin_detail{
  607. template <class T>
  608. T zeta_polynomial_series(T s, T sc, int digits)
  609. {
  610. BOOST_MATH_STD_USING
  611. //
  612. // This is algorithm 3 from:
  613. //
  614. // "An Efficient Algorithm for the Riemann Zeta Function", P. Borwein,
  615. // Canadian Mathematical Society, Conference Proceedings, 2000.
  616. // See: http://www.cecm.sfu.ca/personal/pborwein/PAPERS/P155.pdf
  617. //
  618. BOOST_MATH_STD_USING
  619. int n = (digits * 19) / 53;
  620. T sum = 0;
  621. T two_n = ldexp(T(1), n);
  622. int ej_sign = 1;
  623. for(int j = 0; j < n; ++j)
  624. {
  625. sum += ej_sign * -two_n / pow(T(j + 1), s);
  626. ej_sign = -ej_sign;
  627. }
  628. T ej_sum = 1;
  629. T ej_term = 1;
  630. for(int j = n; j <= 2 * n - 1; ++j)
  631. {
  632. sum += ej_sign * (ej_sum - two_n) / pow(T(j + 1), s);
  633. ej_sign = -ej_sign;
  634. ej_term *= 2 * n - j;
  635. ej_term /= j - n + 1;
  636. ej_sum += ej_term;
  637. }
  638. return -sum / (two_n * (1 - pow(T(2), sc)));
  639. }
  640. template <class T>
  641. T khinchin(int digits)
  642. {
  643. BOOST_MATH_STD_USING
  644. T sum = 0;
  645. T term;
  646. T lim = ldexp(T(1), 1-digits);
  647. T factor = 0;
  648. unsigned last_k = 1;
  649. T num = 1;
  650. for(unsigned n = 1;; ++n)
  651. {
  652. for(unsigned k = last_k; k <= 2 * n - 1; ++k)
  653. {
  654. factor += num / k;
  655. num = -num;
  656. }
  657. last_k = 2 * n;
  658. term = (zeta_polynomial_series(T(2 * n), T(1 - T(2 * n)), digits) - 1) * factor / n;
  659. sum += term;
  660. if(term < lim)
  661. break;
  662. }
  663. return exp(sum / boost::math::constants::ln_two<T, boost::math::policies::policy<> >());
  664. }
  665. }
  666. template <class T>
  667. template<int N>
  668. inline T constant_khinchin<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  669. {
  670. int n = N ? (std::min)(N, tools::digits<T>()) : tools::digits<T>();
  671. return khinchin_detail::khinchin<T>(n);
  672. }
  673. template <class T>
  674. template<int N>
  675. inline T constant_extreme_value_skewness<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  676. { // N[12 Sqrt[6] Zeta[3]/Pi^3, 1101]
  677. BOOST_MATH_STD_USING
  678. T ev(12 * sqrt(static_cast<T>(6)) * zeta_three<T, policies::policy<policies::digits2<N> > >()
  679. / pi_cubed<T, policies::policy<policies::digits2<N> > >() );
  680. //T ev(
  681. //"1.1395470994046486574927930193898461120875997958365518247216557100852480077060706857071875468869385150"
  682. //"1894272048688553376986765366075828644841024041679714157616857834895702411080704529137366329462558680"
  683. //"2015498788776135705587959418756809080074611906006528647805347822929577145038743873949415294942796280"
  684. //"0895597703063466053535550338267721294164578901640163603544404938283861127819804918174973533694090594"
  685. //"3094963822672055237678432023017824416203652657301470473548274848068762500300316769691474974950757965"
  686. //"8640779777748741897542093874605477776538884083378029488863880220988107155275203245233994097178778984"
  687. //"3488995668362387892097897322246698071290011857605809901090220903955815127463328974447572119951192970"
  688. //"3684453635456559086126406960279692862247058250100678008419431185138019869693206366891639436908462809"
  689. //"9756051372711251054914491837034685476095423926553367264355374652153595857163724698198860485357368964"
  690. //"3807049634423621246870868566707915720704996296083373077647528285782964567312903914752617978405994377"
  691. //"9064157147206717895272199736902453130842229559980076472936976287378945035706933650987259357729800315");
  692. return ev;
  693. }
  694. namespace detail{
  695. //
  696. // Calculation of the Glaisher constant depends upon calculating the
  697. // derivative of the zeta function at 2, we can then use the relation:
  698. // zeta'(2) = 1/6 pi^2 [euler + ln(2pi)-12ln(A)]
  699. // To get the constant A.
  700. // See equation 45 at http://mathworld.wolfram.com/RiemannZetaFunction.html.
  701. //
  702. // The derivative of the zeta function is computed by direct differentiation
  703. // of the relation:
  704. // (1-2^(1-s))zeta(s) = SUM(n=0, INF){ (-n)^n / (n+1)^s }
  705. // Which gives us 2 slowly converging but alternating sums to compute,
  706. // for this we use Algorithm 1 from "Convergent Acceleration of Alternating Series",
  707. // Henri Cohen, Fernando Rodriguez Villegas and Don Zagier, Experimental Mathematics 9:1 (1999).
  708. // See http://www.math.utexas.edu/users/villegas/publications/conv-accel.pdf
  709. //
  710. template <class T>
  711. T zeta_series_derivative_2(unsigned digits)
  712. {
  713. // Derivative of the series part, evaluated at 2:
  714. BOOST_MATH_STD_USING
  715. int n = digits * 301 * 13 / 10000;
  716. T d = pow(3 + sqrt(T(8)), n);
  717. d = (d + 1 / d) / 2;
  718. T b = -1;
  719. T c = -d;
  720. T s = 0;
  721. for(int k = 0; k < n; ++k)
  722. {
  723. T a = -log(T(k+1)) / ((k+1) * (k+1));
  724. c = b - c;
  725. s = s + c * a;
  726. b = (k + n) * (k - n) * b / ((k + T(0.5f)) * (k + 1));
  727. }
  728. return s / d;
  729. }
  730. template <class T>
  731. T zeta_series_2(unsigned digits)
  732. {
  733. // Series part of zeta at 2:
  734. BOOST_MATH_STD_USING
  735. int n = digits * 301 * 13 / 10000;
  736. T d = pow(3 + sqrt(T(8)), n);
  737. d = (d + 1 / d) / 2;
  738. T b = -1;
  739. T c = -d;
  740. T s = 0;
  741. for(int k = 0; k < n; ++k)
  742. {
  743. T a = T(1) / ((k + 1) * (k + 1));
  744. c = b - c;
  745. s = s + c * a;
  746. b = (k + n) * (k - n) * b / ((k + T(0.5f)) * (k + 1));
  747. }
  748. return s / d;
  749. }
  750. template <class T>
  751. inline T zeta_series_lead_2()
  752. {
  753. // lead part at 2:
  754. return 2;
  755. }
  756. template <class T>
  757. inline T zeta_series_derivative_lead_2()
  758. {
  759. // derivative of lead part at 2:
  760. return -2 * boost::math::constants::ln_two<T>();
  761. }
  762. template <class T>
  763. inline T zeta_derivative_2(unsigned n)
  764. {
  765. // zeta derivative at 2:
  766. return zeta_series_derivative_2<T>(n) * zeta_series_lead_2<T>()
  767. + zeta_series_derivative_lead_2<T>() * zeta_series_2<T>(n);
  768. }
  769. } // namespace detail
  770. template <class T>
  771. template<int N>
  772. inline T constant_glaisher<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  773. {
  774. BOOST_MATH_STD_USING
  775. typedef policies::policy<policies::digits2<N> > forwarding_policy;
  776. int n = N ? (std::min)(N, tools::digits<T>()) : tools::digits<T>();
  777. T v = detail::zeta_derivative_2<T>(n);
  778. v *= 6;
  779. v /= boost::math::constants::pi<T, forwarding_policy>() * boost::math::constants::pi<T, forwarding_policy>();
  780. v -= boost::math::constants::euler<T, forwarding_policy>();
  781. v -= log(2 * boost::math::constants::pi<T, forwarding_policy>());
  782. v /= -12;
  783. return exp(v);
  784. /*
  785. // from http://mpmath.googlecode.com/svn/data/glaisher.txt
  786. // 20,000 digits of the Glaisher-Kinkelin constant A = exp(1/2 - zeta'(-1))
  787. // Computed using A = exp((6 (-zeta'(2))/pi^2 + log 2 pi + gamma)/12)
  788. // with Euler-Maclaurin summation for zeta'(2).
  789. T g(
  790. "1.282427129100622636875342568869791727767688927325001192063740021740406308858826"
  791. "46112973649195820237439420646120399000748933157791362775280404159072573861727522"
  792. "14334327143439787335067915257366856907876561146686449997784962754518174312394652"
  793. "76128213808180219264516851546143919901083573730703504903888123418813674978133050"
  794. "93770833682222494115874837348064399978830070125567001286994157705432053927585405"
  795. "81731588155481762970384743250467775147374600031616023046613296342991558095879293"
  796. "36343887288701988953460725233184702489001091776941712153569193674967261270398013"
  797. "52652668868978218897401729375840750167472114895288815996668743164513890306962645"
  798. "59870469543740253099606800842447417554061490189444139386196089129682173528798629"
  799. "88434220366989900606980888785849587494085307347117090132667567503310523405221054"
  800. "14176776156308191919997185237047761312315374135304725819814797451761027540834943"
  801. "14384965234139453373065832325673954957601692256427736926358821692159870775858274"
  802. "69575162841550648585890834128227556209547002918593263079373376942077522290940187");
  803. return g;
  804. */
  805. }
  806. template <class T>
  807. template<int N>
  808. inline T constant_rayleigh_skewness<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  809. { // 1100 digits of the Rayleigh distribution skewness
  810. // N[2 Sqrt[Pi] (Pi - 3)/((4 - Pi)^(3/2)), 1100]
  811. BOOST_MATH_STD_USING
  812. T rs(2 * root_pi<T, policies::policy<policies::digits2<N> > >()
  813. * pi_minus_three<T, policies::policy<policies::digits2<N> > >()
  814. / pow(four_minus_pi<T, policies::policy<policies::digits2<N> > >(), static_cast<T>(3./2))
  815. );
  816. // 6.31110657818937138191899351544227779844042203134719497658094585692926819617473725459905027032537306794400047264,
  817. //"0.6311106578189371381918993515442277798440422031347194976580945856929268196174737254599050270325373067"
  818. //"9440004726436754739597525250317640394102954301685809920213808351450851396781817932734836994829371322"
  819. //"5797376021347531983451654130317032832308462278373358624120822253764532674177325950686466133508511968"
  820. //"2389168716630349407238090652663422922072397393006683401992961569208109477307776249225072042971818671"
  821. //"4058887072693437217879039875871765635655476241624825389439481561152126886932506682176611183750503553"
  822. //"1218982627032068396407180216351425758181396562859085306247387212297187006230007438534686340210168288"
  823. //"8956816965453815849613622117088096547521391672977226658826566757207615552041767516828171274858145957"
  824. //"6137539156656005855905288420585194082284972984285863898582313048515484073396332610565441264220790791"
  825. //"0194897267890422924599776483890102027823328602965235306539844007677157873140562950510028206251529523"
  826. //"7428049693650605954398446899724157486062545281504433364675815915402937209673727753199567661561209251"
  827. //"4695589950526053470201635372590001578503476490223746511106018091907936826431407434894024396366284848"); ;
  828. return rs;
  829. }
  830. template <class T>
  831. template<int N>
  832. inline T constant_rayleigh_kurtosis_excess<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  833. { // - (6 Pi^2 - 24 Pi + 16)/((Pi - 4)^2)
  834. // Might provide and calculate this using pi_minus_four.
  835. BOOST_MATH_STD_USING
  836. return - (((static_cast<T>(6) * pi<T, policies::policy<policies::digits2<N> > >()
  837. * pi<T, policies::policy<policies::digits2<N> > >())
  838. - (static_cast<T>(24) * pi<T, policies::policy<policies::digits2<N> > >()) + static_cast<T>(16) )
  839. /
  840. ((pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4))
  841. * (pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4)))
  842. );
  843. }
  844. template <class T>
  845. template<int N>
  846. inline T constant_rayleigh_kurtosis<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  847. { // 3 - (6 Pi^2 - 24 Pi + 16)/((Pi - 4)^2)
  848. // Might provide and calculate this using pi_minus_four.
  849. BOOST_MATH_STD_USING
  850. return static_cast<T>(3) - (((static_cast<T>(6) * pi<T, policies::policy<policies::digits2<N> > >()
  851. * pi<T, policies::policy<policies::digits2<N> > >())
  852. - (static_cast<T>(24) * pi<T, policies::policy<policies::digits2<N> > >()) + static_cast<T>(16) )
  853. /
  854. ((pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4))
  855. * (pi<T, policies::policy<policies::digits2<N> > >() - static_cast<T>(4)))
  856. );
  857. }
  858. template <class T>
  859. template<int N>
  860. inline T constant_log2_e<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  861. {
  862. return 1 / boost::math::constants::ln_two<T>();
  863. }
  864. template <class T>
  865. template<int N>
  866. inline T constant_quarter_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  867. {
  868. return boost::math::constants::pi<T>() / 4;
  869. }
  870. template <class T>
  871. template<int N>
  872. inline T constant_one_div_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  873. {
  874. return 1 / boost::math::constants::pi<T>();
  875. }
  876. template <class T>
  877. template<int N>
  878. inline T constant_two_div_root_pi<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  879. {
  880. return 2 * boost::math::constants::one_div_root_pi<T>();
  881. }
  882. #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
  883. template <class T>
  884. template<int N>
  885. inline T constant_first_feigenbaum<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  886. {
  887. // We know the constant to 1018 decimal digits.
  888. // See: http://www.plouffe.fr/simon/constants/feigenbaum.txt
  889. // Also: https://oeis.org/A006890
  890. // N is in binary digits; so we multiply by log_2(10)
  891. BOOST_STATIC_ASSERT_MSG(N < 3.321*1018, "\nThe first Feigenbaum constant cannot be computed at runtime; it is too expensive. It is known to 1018 decimal digits; you must request less than that.");
  892. T alpha{"4.6692016091029906718532038204662016172581855774757686327456513430041343302113147371386897440239480138171659848551898151344086271420279325223124429888908908599449354632367134115324817142199474556443658237932020095610583305754586176522220703854106467494942849814533917262005687556659523398756038256372256480040951071283890611844702775854285419801113440175002428585382498335715522052236087250291678860362674527213399057131606875345083433934446103706309452019115876972432273589838903794946257251289097948986768334611626889116563123474460575179539122045562472807095202198199094558581946136877445617396074115614074243754435499204869180982648652368438702799649017397793425134723808737136211601860128186102056381818354097598477964173900328936171432159878240789776614391395764037760537119096932066998361984288981837003229412030210655743295550388845849737034727532121925706958414074661841981961006129640161487712944415901405467941800198133253378592493365883070459999938375411726563553016862529032210862320550634510679399023341675"};
  893. return alpha;
  894. }
  895. template <class T>
  896. template<int N>
  897. inline T constant_plastic<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  898. {
  899. using std::cbrt;
  900. using std::sqrt;
  901. return (cbrt(9-sqrt(T(69))) + cbrt(9+sqrt(T(69))))/cbrt(T(18));
  902. }
  903. template <class T>
  904. template<int N>
  905. inline T constant_gauss<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  906. {
  907. using std::sqrt;
  908. T a = sqrt(T(2));
  909. T g = 1;
  910. const T scale = sqrt(std::numeric_limits<T>::epsilon())/512;
  911. while (a-g > scale*g)
  912. {
  913. T anp1 = (a + g)/2;
  914. g = sqrt(a*g);
  915. a = anp1;
  916. }
  917. return 2/(a + g);
  918. }
  919. template <class T>
  920. template<int N>
  921. inline T constant_dottie<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  922. {
  923. // Error analysis: cos(x(1+d)) - x(1+d) = -(sin(x)+1)xd; plug in x = 0.739 gives -1.236d; take d as half an ulp gives the termination criteria we want.
  924. using std::cos;
  925. using std::abs;
  926. using std::sin;
  927. T x{".739085133215160641655312087673873404013411758900757464965680635773284654883547594599376106931766531849801246"};
  928. T residual = cos(x) - x;
  929. do {
  930. x += residual/(sin(x)+1);
  931. residual = cos(x) - x;
  932. } while(abs(residual) > std::numeric_limits<T>::epsilon());
  933. return x;
  934. }
  935. template <class T>
  936. template<int N>
  937. inline T constant_reciprocal_fibonacci<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  938. {
  939. // Wikipedia says Gosper has deviced a faster algorithm for this, but I read the linked paper and couldn't see it!
  940. // In any case, k bits per iteration is fine, though it would be better to sum from smallest to largest.
  941. // That said, the condition number is unity, so it should be fine.
  942. T x0 = 1;
  943. T x1 = 1;
  944. T sum = 2;
  945. T diff = 1;
  946. while (diff > std::numeric_limits<T>::epsilon()) {
  947. T tmp = x1 + x0;
  948. diff = 1/tmp;
  949. sum += diff;
  950. x0 = x1;
  951. x1 = tmp;
  952. }
  953. return sum;
  954. }
  955. template <class T>
  956. template<int N>
  957. inline T constant_laplace_limit<T>::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant<int, N>)))
  958. {
  959. // If x is the exact root, then the approximate root is given by x(1+delta).
  960. // Plugging this into the equation for the Laplace limit gives the residual of approximately
  961. // 2.6389delta. Take delta as half an epsilon and give some leeway so we don't get caught in an infinite loop,
  962. // gives a termination condition as 2eps.
  963. using std::abs;
  964. using std::exp;
  965. using std::sqrt;
  966. T x{"0.66274341934918158097474209710925290705623354911502241752039253499097185308651127724965480259895818168"};
  967. T tmp = sqrt(1+x*x);
  968. T etmp = exp(tmp);
  969. T residual = x*exp(tmp) - 1 - tmp;
  970. T df = etmp -x/tmp + etmp*x*x/tmp;
  971. do {
  972. x -= residual/df;
  973. tmp = sqrt(1+x*x);
  974. etmp = exp(tmp);
  975. residual = x*exp(tmp) - 1 - tmp;
  976. df = etmp -x/tmp + etmp*x*x/tmp;
  977. } while(abs(residual) > 2*std::numeric_limits<T>::epsilon());
  978. return x;
  979. }
  980. #endif
  981. }
  982. }
  983. }
  984. } // namespaces
  985. #endif // BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED