constants.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2011 John Maddock. Distributed under the Boost
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // This file has no include guards or namespaces - it's expanded inline inside default_ops.hpp
  7. //
  8. template <class T>
  9. void calc_log2(T& num, unsigned digits)
  10. {
  11. typedef typename boost::multiprecision::detail::canonical<boost::uint32_t, T>::type ui_type;
  12. typedef typename mpl::front<typename T::signed_types>::type si_type;
  13. //
  14. // String value with 1100 digits:
  15. //
  16. static const char* string_val = "0."
  17. "6931471805599453094172321214581765680755001343602552541206800094933936219696947156058633269964186875"
  18. "4200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335"
  19. "0115364497955239120475172681574932065155524734139525882950453007095326366642654104239157814952043740"
  20. "4303855008019441706416715186447128399681717845469570262716310645461502572074024816377733896385506952"
  21. "6066834113727387372292895649354702576265209885969320196505855476470330679365443254763274495125040606"
  22. "9438147104689946506220167720424524529612687946546193165174681392672504103802546259656869144192871608"
  23. "2938031727143677826548775664850856740776484514644399404614226031930967354025744460703080960850474866"
  24. "3852313818167675143866747664789088143714198549423151997354880375165861275352916610007105355824987941"
  25. "4729509293113897155998205654392871700072180857610252368892132449713893203784393530887748259701715591"
  26. "0708823683627589842589185353024363421436706118923678919237231467232172053401649256872747782344535347"
  27. "6481149418642386776774406069562657379600867076257199184734022651462837904883062033061144630073719489";
  28. //
  29. // Check if we can just construct from string:
  30. //
  31. if (digits < 3640) // 3640 binary digits ~ 1100 decimal digits
  32. {
  33. num = string_val;
  34. return;
  35. }
  36. //
  37. // We calculate log2 from using the formula:
  38. //
  39. // ln(2) = 3/4 SUM[n>=0] ((-1)^n * N!^2 / (2^n(2n+1)!))
  40. //
  41. // Numerator and denominator are calculated separately and then
  42. // divided at the end, we also precalculate the terms up to n = 5
  43. // since these fit in a 32-bit integer anyway.
  44. //
  45. // See Gourdon, X., and Sebah, P. The logarithmic constant: log 2, Jan. 2004.
  46. // Also http://www.mpfr.org/algorithms.pdf.
  47. //
  48. num = static_cast<ui_type>(1180509120uL);
  49. T denom, next_term, temp;
  50. denom = static_cast<ui_type>(1277337600uL);
  51. next_term = static_cast<ui_type>(120uL);
  52. si_type sign = -1;
  53. ui_type limit = digits / 3 + 1;
  54. for (ui_type n = 6; n < limit; ++n)
  55. {
  56. temp = static_cast<ui_type>(2);
  57. eval_multiply(temp, ui_type(2 * n));
  58. eval_multiply(temp, ui_type(2 * n + 1));
  59. eval_multiply(num, temp);
  60. eval_multiply(denom, temp);
  61. sign = -sign;
  62. eval_multiply(next_term, n);
  63. eval_multiply(temp, next_term, next_term);
  64. if (sign < 0)
  65. temp.negate();
  66. eval_add(num, temp);
  67. }
  68. eval_multiply(denom, ui_type(4));
  69. eval_multiply(num, ui_type(3));
  70. INSTRUMENT_BACKEND(denom);
  71. INSTRUMENT_BACKEND(num);
  72. eval_divide(num, denom);
  73. INSTRUMENT_BACKEND(num);
  74. }
  75. template <class T>
  76. void calc_e(T& result, unsigned digits)
  77. {
  78. typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
  79. //
  80. // 1100 digits in string form:
  81. //
  82. const char* string_val = "2."
  83. "7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274"
  84. "2746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901"
  85. "1573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069"
  86. "5517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416"
  87. "9283681902551510865746377211125238978442505695369677078544996996794686445490598793163688923009879312"
  88. "7736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117"
  89. "3012381970684161403970198376793206832823764648042953118023287825098194558153017567173613320698112509"
  90. "9618188159304169035159888851934580727386673858942287922849989208680582574927961048419844436346324496"
  91. "8487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016"
  92. "7683964243781405927145635490613031072085103837505101157477041718986106873969655212671546889570350354"
  93. "0212340784981933432106817012100562788023519303322474501585390473041995777709350366041699732972508869";
  94. //
  95. // Check if we can just construct from string:
  96. //
  97. if (digits < 3640) // 3640 binary digits ~ 1100 decimal digits
  98. {
  99. result = string_val;
  100. return;
  101. }
  102. T lim;
  103. lim = ui_type(1);
  104. eval_ldexp(lim, lim, digits);
  105. //
  106. // Standard evaluation from the definition of e: http://functions.wolfram.com/Constants/E/02/
  107. //
  108. result = ui_type(2);
  109. T denom;
  110. denom = ui_type(1);
  111. ui_type i = 2;
  112. do
  113. {
  114. eval_multiply(denom, i);
  115. eval_multiply(result, i);
  116. eval_add(result, ui_type(1));
  117. ++i;
  118. } while (denom.compare(lim) <= 0);
  119. eval_divide(result, denom);
  120. }
  121. template <class T>
  122. void calc_pi(T& result, unsigned digits)
  123. {
  124. typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
  125. typedef typename mpl::front<typename T::float_types>::type real_type;
  126. //
  127. // 1100 digits in string form:
  128. //
  129. const char* string_val = "3."
  130. "1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
  131. "8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196"
  132. "4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273"
  133. "7245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094"
  134. "3305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912"
  135. "9833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132"
  136. "0005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235"
  137. "4201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859"
  138. "5024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303"
  139. "5982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989"
  140. "3809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913152";
  141. //
  142. // Check if we can just construct from string:
  143. //
  144. if (digits < 3640) // 3640 binary digits ~ 1100 decimal digits
  145. {
  146. result = string_val;
  147. return;
  148. }
  149. T a;
  150. a = ui_type(1);
  151. T b;
  152. T A(a);
  153. T B;
  154. B = real_type(0.5f);
  155. T D;
  156. D = real_type(0.25f);
  157. T lim;
  158. lim = ui_type(1);
  159. eval_ldexp(lim, lim, -(int)digits);
  160. //
  161. // This algorithm is from:
  162. // Schonhage, A., Grotefeld, A. F. W., and Vetter, E. Fast Algorithms: A Multitape Turing
  163. // Machine Implementation. BI Wissenschaftverlag, 1994.
  164. // Also described in MPFR's algorithm guide: http://www.mpfr.org/algorithms.pdf.
  165. //
  166. // Let:
  167. // a[0] = A[0] = 1
  168. // B[0] = 1/2
  169. // D[0] = 1/4
  170. // Then:
  171. // S[k+1] = (A[k]+B[k]) / 4
  172. // b[k] = sqrt(B[k])
  173. // a[k+1] = a[k]^2
  174. // B[k+1] = 2(A[k+1]-S[k+1])
  175. // D[k+1] = D[k] - 2^k(A[k+1]-B[k+1])
  176. // Stop when |A[k]-B[k]| <= 2^(k-p)
  177. // and PI = B[k]/D[k]
  178. unsigned k = 1;
  179. do
  180. {
  181. eval_add(result, A, B);
  182. eval_ldexp(result, result, -2);
  183. eval_sqrt(b, B);
  184. eval_add(a, b);
  185. eval_ldexp(a, a, -1);
  186. eval_multiply(A, a, a);
  187. eval_subtract(B, A, result);
  188. eval_ldexp(B, B, 1);
  189. eval_subtract(result, A, B);
  190. bool neg = eval_get_sign(result) < 0;
  191. if (neg)
  192. result.negate();
  193. if (result.compare(lim) <= 0)
  194. break;
  195. if (neg)
  196. result.negate();
  197. eval_ldexp(result, result, k - 1);
  198. eval_subtract(D, result);
  199. ++k;
  200. eval_ldexp(lim, lim, 1);
  201. } while (true);
  202. eval_divide(result, B, D);
  203. }
  204. template <class T, const T& (*F)(void)>
  205. struct constant_initializer
  206. {
  207. static void do_nothing()
  208. {
  209. init.do_nothing();
  210. }
  211. private:
  212. struct initializer
  213. {
  214. initializer()
  215. {
  216. F();
  217. }
  218. void do_nothing() const {}
  219. };
  220. static const initializer init;
  221. };
  222. template <class T, const T& (*F)(void)>
  223. typename constant_initializer<T, F>::initializer const constant_initializer<T, F>::init;
  224. template <class T>
  225. const T& get_constant_ln2()
  226. {
  227. static BOOST_MP_THREAD_LOCAL T result;
  228. static BOOST_MP_THREAD_LOCAL long digits = 0;
  229. #ifndef BOOST_MP_USING_THREAD_LOCAL
  230. static BOOST_MP_THREAD_LOCAL bool b = false;
  231. constant_initializer<T, &get_constant_ln2<T> >::do_nothing();
  232. if (!b || (digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  233. {
  234. b = true;
  235. #else
  236. if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  237. {
  238. #endif
  239. calc_log2(result, boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  240. digits = boost::multiprecision::detail::digits2<number<T> >::value();
  241. }
  242. return result;
  243. }
  244. template <class T>
  245. const T& get_constant_e()
  246. {
  247. static BOOST_MP_THREAD_LOCAL T result;
  248. static BOOST_MP_THREAD_LOCAL long digits = 0;
  249. #ifndef BOOST_MP_USING_THREAD_LOCAL
  250. static BOOST_MP_THREAD_LOCAL bool b = false;
  251. constant_initializer<T, &get_constant_e<T> >::do_nothing();
  252. if (!b || (digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  253. {
  254. b = true;
  255. #else
  256. if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  257. {
  258. #endif
  259. calc_e(result, boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  260. digits = boost::multiprecision::detail::digits2<number<T> >::value();
  261. }
  262. return result;
  263. }
  264. template <class T>
  265. const T& get_constant_pi()
  266. {
  267. static BOOST_MP_THREAD_LOCAL T result;
  268. static BOOST_MP_THREAD_LOCAL long digits = 0;
  269. #ifndef BOOST_MP_USING_THREAD_LOCAL
  270. static BOOST_MP_THREAD_LOCAL bool b = false;
  271. constant_initializer<T, &get_constant_pi<T> >::do_nothing();
  272. if (!b || (digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  273. {
  274. b = true;
  275. #else
  276. if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  277. {
  278. #endif
  279. calc_pi(result, boost::multiprecision::detail::digits2<number<T, et_on> >::value());
  280. digits = boost::multiprecision::detail::digits2<number<T> >::value();
  281. }
  282. return result;
  283. }
  284. template <class T>
  285. const T& get_constant_one_over_epsilon()
  286. {
  287. static BOOST_MP_THREAD_LOCAL T result;
  288. static BOOST_MP_THREAD_LOCAL long digits = 0;
  289. #ifndef BOOST_MP_USING_THREAD_LOCAL
  290. static BOOST_MP_THREAD_LOCAL bool b = false;
  291. constant_initializer<T, &get_constant_one_over_epsilon<T> >::do_nothing();
  292. if (!b || (digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  293. {
  294. b = true;
  295. #else
  296. if ((digits != boost::multiprecision::detail::digits2<number<T> >::value()))
  297. {
  298. #endif
  299. typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
  300. result = static_cast<ui_type>(1u);
  301. eval_divide(result, std::numeric_limits<number<T> >::epsilon().backend());
  302. }
  303. return result;
  304. }