calculate_constants.hpp 37 KB

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