calculate_constants.hpp 37 KB

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