floating_point_examples.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. #include <boost/math/constants/constants.hpp>
  6. #include <boost/multiprecision/cpp_dec_float.hpp>
  7. #include <boost/math/special_functions/gamma.hpp>
  8. #include <boost/math/special_functions/bessel.hpp>
  9. #include <iostream>
  10. #include <iomanip>
  11. #if !defined(BOOST_NO_CXX11_HDR_ARRAY) && !defined(BOOST_NO_CXX11_LAMBDAS) && !(defined(CI_SUPPRESS_KNOWN_ISSUES) && defined(__GNUC__) && defined(_WIN32))
  12. #include <array>
  13. //[AOS1
  14. /*`Generic numeric programming employs templates to use the same code for different
  15. floating-point types and functions. Consider the area of a circle a of radius r, given by
  16. [:['a = [pi] * r[super 2]]]
  17. The area of a circle can be computed in generic programming using Boost.Math
  18. for the constant [pi] as shown below:
  19. */
  20. //=#include <boost/math/constants/constants.hpp>
  21. template<typename T>
  22. inline T area_of_a_circle(T r)
  23. {
  24. using boost::math::constants::pi;
  25. return pi<T>() * r * r;
  26. }
  27. /*`
  28. It is possible to use `area_of_a_circle()` with built-in floating-point types as
  29. well as floating-point types from Boost.Multiprecision. In particular, consider a
  30. system with 4-byte single-precision float, 8-byte double-precision double and also the
  31. `cpp_dec_float_50` data type from Boost.Multiprecision with 50 decimal digits
  32. of precision.
  33. We can compute and print the approximate area of a circle with radius 123/100 for
  34. `float`, `double` and `cpp_dec_float_50` with the program below.
  35. */
  36. //]
  37. //[AOS3
  38. /*`In the next example we'll look at calling both standard library and Boost.Math functions from within generic code.
  39. We'll also show how to cope with template arguments which are expression-templates rather than number types.*/
  40. //]
  41. //[JEL
  42. /*`
  43. In this example we'll show several implementations of the
  44. [@http://mathworld.wolfram.com/LambdaFunction.html Jahnke and Emden Lambda function],
  45. each implementation a little more sophisticated than the last.
  46. The Jahnke-Emden Lambda function is defined by the equation:
  47. [:['JahnkeEmden(v, z) = [Gamma](v+1) * J[sub v](z) / (z / 2)[super v]]]
  48. If we were to implement this at double precision using Boost.Math's facilities for the Gamma and Bessel
  49. function calls it would look like this:
  50. */
  51. double JEL1(double v, double z)
  52. {
  53. return boost::math::tgamma(v + 1) * boost::math::cyl_bessel_j(v, z) / std::pow(z / 2, v);
  54. }
  55. /*`
  56. Calling this function as:
  57. std::cout << std::scientific << std::setprecision(std::numeric_limits<double>::digits10);
  58. std::cout << JEL1(2.5, 0.5) << std::endl;
  59. Yields the output:
  60. [pre 9.822663964796047e-001]
  61. Now let's implement the function again, but this time using the multiprecision type
  62. `cpp_dec_float_50` as the argument type:
  63. */
  64. boost::multiprecision::cpp_dec_float_50
  65. JEL2(boost::multiprecision::cpp_dec_float_50 v, boost::multiprecision::cpp_dec_float_50 z)
  66. {
  67. return boost::math::tgamma(v + 1) * boost::math::cyl_bessel_j(v, z) / boost::multiprecision::pow(z / 2, v);
  68. }
  69. /*`
  70. The implementation is almost the same as before, but with one key difference - we can no longer call
  71. `std::pow`, instead we must call the version inside the `boost::multiprecision` namespace. In point of
  72. fact, we could have omitted the namespace prefix on the call to `pow` since the right overload would
  73. have been found via [@http://en.wikipedia.org/wiki/Argument-dependent_name_lookup
  74. argument dependent lookup] in any case.
  75. Note also that the first argument to `pow` along with the argument to `tgamma` in the above code
  76. are actually expression templates. The `pow` and `tgamma` functions will handle these arguments
  77. just fine.
  78. Here's an example of how the function may be called:
  79. std::cout << std::scientific << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10);
  80. std::cout << JEL2(cpp_dec_float_50(2.5), cpp_dec_float_50(0.5)) << std::endl;
  81. Which outputs:
  82. [pre 9.82266396479604757017335009796882833995903762577173e-01]
  83. Now that we've seen some non-template examples, lets repeat the code again, but this time as a template
  84. that can be called either with a builtin type (`float`, `double` etc), or with a multiprecision type:
  85. */
  86. template <class Float>
  87. Float JEL3(Float v, Float z)
  88. {
  89. using std::pow;
  90. return boost::math::tgamma(v + 1) * boost::math::cyl_bessel_j(v, z) / pow(z / 2, v);
  91. }
  92. /*`
  93. Once again the code is almost the same as before, but the call to `pow` has changed yet again.
  94. We need the call to resolve to either `std::pow` (when the argument is a builtin type), or
  95. to `boost::multiprecision::pow` (when the argument is a multiprecision type). We do that by
  96. making the call unqualified so that versions of `pow` defined in the same namespace as type
  97. `Float` are found via argument dependent lookup, while the `using std::pow` directive makes
  98. the standard library versions visible for builtin floating point types.
  99. Let's call the function with both `double` and multiprecision arguments:
  100. std::cout << std::scientific << std::setprecision(std::numeric_limits<double>::digits10);
  101. std::cout << JEL3(2.5, 0.5) << std::endl;
  102. std::cout << std::scientific << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10);
  103. std::cout << JEL3(cpp_dec_float_50(2.5), cpp_dec_float_50(0.5)) << std::endl;
  104. Which outputs:
  105. [pre
  106. 9.822663964796047e-001
  107. 9.82266396479604757017335009796882833995903762577173e-01
  108. ]
  109. Unfortunately there is a problem with this version: if we were to call it like this:
  110. boost::multiprecision::cpp_dec_float_50 v(2), z(0.5);
  111. JEL3(v + 0.5, z);
  112. Then we would get a long and inscrutable error message from the compiler: the problem here is that the first
  113. argument to `JEL3` is not a number type, but an expression template. We could obviously add a typecast to
  114. fix the issue:
  115. JEL(cpp_dec_float_50(v + 0.5), z);
  116. However, if we want the function JEL to be truly reusable, then a better solution might be preferred.
  117. To achieve this we can borrow some code from Boost.Math which calculates the return type of mixed-argument
  118. functions, here's how the new code looks now:
  119. */
  120. template <class Float1, class Float2>
  121. typename boost::math::tools::promote_args<Float1, Float2>::type
  122. JEL4(Float1 v, Float2 z)
  123. {
  124. using std::pow;
  125. return boost::math::tgamma(v + 1) * boost::math::cyl_bessel_j(v, z) / pow(z / 2, v);
  126. }
  127. /*`
  128. As you can see the two arguments to the function are now separate template types, and
  129. the return type is computed using the `promote_args` metafunction from Boost.Math.
  130. Now we can call:
  131. std::cout << std::scientific << std::setprecision(std::numeric_limits<cpp_dec_float_100>::digits10);
  132. std::cout << JEL4(cpp_dec_float_100(2) + 0.5, cpp_dec_float_100(0.5)) << std::endl;
  133. And get 100 digits of output:
  134. [pre 9.8226639647960475701733500979688283399590376257717309069410413822165082248153638454147004236848917775e-01]
  135. As a bonus, we can now call the function not just with expression templates, but with other mixed types as well:
  136. for example `float` and `double` or `int` and `double`, and the correct return type will be computed in each case.
  137. Note that while in this case we didn't have to change the body of the function, in the general case
  138. any function like this which creates local variables internally would have to use `promote_args`
  139. to work out what type those variables should be, for example:
  140. template <class Float1, class Float2>
  141. typename boost::math::tools::promote_args<Float1, Float2>::type
  142. JEL5(Float1 v, Float2 z)
  143. {
  144. using std::pow;
  145. typedef typename boost::math::tools::promote_args<Float1, Float2>::type variable_type;
  146. variable_type t = pow(z / 2, v);
  147. return boost::math::tgamma(v + 1) * boost::math::cyl_bessel_j(v, z) / t;
  148. }
  149. */
  150. //]
  151. //[ND1
  152. /*`
  153. In this example we'll add even more power to generic numeric programming using not only different
  154. floating-point types but also function objects as template parameters. Consider
  155. some well-known central difference rules for numerically computing the first derivative
  156. of a function ['f[prime](x)] with ['x [isin] [real]]:
  157. [equation floating_point_eg1]
  158. Where the difference terms ['m[sub n]] are given by:
  159. [equation floating_point_eg2]
  160. and ['dx] is the step-size of the derivative.
  161. The third formula in Equation 1 is a three-point central difference rule. It calculates
  162. the first derivative of ['f[prime](x)] to ['O(dx[super 6])], where ['dx] is the given step-size.
  163. For example, if
  164. the step-size is 0.01 this derivative calculation has about 6 decimal digits of precision -
  165. just about right for the 7 decimal digits of single-precision float.
  166. Let's make a generic template subroutine using this three-point central difference
  167. rule. In particular:
  168. */
  169. template<typename value_type, typename function_type>
  170. value_type derivative(const value_type x, const value_type dx, function_type func)
  171. {
  172. // Compute d/dx[func(*first)] using a three-point
  173. // central difference rule of O(dx^6).
  174. const value_type dx1 = dx;
  175. const value_type dx2 = dx1 * 2;
  176. const value_type dx3 = dx1 * 3;
  177. const value_type m1 = (func(x + dx1) - func(x - dx1)) / 2;
  178. const value_type m2 = (func(x + dx2) - func(x - dx2)) / 4;
  179. const value_type m3 = (func(x + dx3) - func(x - dx3)) / 6;
  180. const value_type fifteen_m1 = 15 * m1;
  181. const value_type six_m2 = 6 * m2;
  182. const value_type ten_dx1 = 10 * dx1;
  183. return ((fifteen_m1 - six_m2) + m3) / ten_dx1;
  184. }
  185. /*`The `derivative()` template function can be used to compute the first derivative
  186. of any function to ['O(dx[super 6])]. For example, consider the first derivative of ['sin(x)] evaluated
  187. at ['x = [pi]/3]. In other words,
  188. [equation floating_point_eg3]
  189. The code below computes the derivative in Equation 3 for float, double and boost's
  190. multiple-precision type cpp_dec_float_50.
  191. */
  192. //]
  193. //[GI1
  194. /*`
  195. Similar to the generic derivative example, we can calculate integrals in a similar manner:
  196. */
  197. template<typename value_type, typename function_type>
  198. inline value_type integral(const value_type a,
  199. const value_type b,
  200. const value_type tol,
  201. function_type func)
  202. {
  203. unsigned n = 1U;
  204. value_type h = (b - a);
  205. value_type I = (func(a) + func(b)) * (h / 2);
  206. for(unsigned k = 0U; k < 8U; k++)
  207. {
  208. h /= 2;
  209. value_type sum(0);
  210. for(unsigned j = 1U; j <= n; j++)
  211. {
  212. sum += func(a + (value_type((j * 2) - 1) * h));
  213. }
  214. const value_type I0 = I;
  215. I = (I / 2) + (h * sum);
  216. const value_type ratio = I0 / I;
  217. const value_type delta = ratio - 1;
  218. const value_type delta_abs = ((delta < 0) ? -delta : delta);
  219. if((k > 1U) && (delta_abs < tol))
  220. {
  221. break;
  222. }
  223. n *= 2U;
  224. }
  225. return I;
  226. }
  227. /*`
  228. The following sample program shows how the function can be called, we begin
  229. by defining a function object, which when integrated should yield the Bessel J
  230. function:
  231. */
  232. template<typename value_type>
  233. class cyl_bessel_j_integral_rep
  234. {
  235. public:
  236. cyl_bessel_j_integral_rep(const unsigned N,
  237. const value_type& X) : n(N), x(X) { }
  238. value_type operator()(const value_type& t) const
  239. {
  240. // pi * Jn(x) = Int_0^pi [cos(x * sin(t) - n*t) dt]
  241. return cos(x * sin(t) - (n * t));
  242. }
  243. private:
  244. const unsigned n;
  245. const value_type x;
  246. };
  247. //]
  248. //[POLY
  249. /*`
  250. In this example we'll look at polynomial evaluation, this is not only an important
  251. use case, but it's one that `number` performs particularly well at because the
  252. expression templates ['completely eliminate all temporaries] from a
  253. [@http://en.wikipedia.org/wiki/Horner%27s_method Horner polynomial
  254. evaluation scheme].
  255. The following code evaluates `sin(x)` as a polynomial, accurate to at least 64 decimal places:
  256. */
  257. using boost::multiprecision::cpp_dec_float;
  258. typedef boost::multiprecision::number<cpp_dec_float<64> > mp_type;
  259. mp_type mysin(const mp_type& x)
  260. {
  261. // Approximation of sin(x * pi/2) for -1 <= x <= 1, using an order 63 polynomial.
  262. static const std::array<mp_type, 32U> coefs =
  263. {{
  264. mp_type("+1.5707963267948966192313216916397514420985846996875529104874722961539082031431044993140174126711"), //"),
  265. mp_type("-0.64596409750624625365575656389794573337969351178927307696134454382929989411386887578263960484"), // ^3
  266. mp_type("+0.07969262624616704512050554949047802252091164235106119545663865720995702920146198554317279"), // ^5
  267. mp_type("-0.0046817541353186881006854639339534378594950280185010575749538605102665157913157426229824"), // ^7
  268. mp_type("+0.00016044118478735982187266087016347332970280754062061156858775174056686380286868007443"), // ^9
  269. mp_type("-3.598843235212085340458540018208389404888495232432127661083907575106196374913134E-6"), // ^11
  270. mp_type("+5.692172921967926811775255303592184372902829756054598109818158853197797542565E-8"), // ^13
  271. mp_type("-6.688035109811467232478226335783138689956270985704278659373558497256423498E-10"), // ^15
  272. mp_type("+6.066935731106195667101445665327140070166203261129845646380005577490472E-12"), // ^17
  273. mp_type("-4.377065467313742277184271313776319094862897030084226361576452003432E-14"), // ^19
  274. mp_type("+2.571422892860473866153865950420487369167895373255729246889168337E-16"), // ^21
  275. mp_type("-1.253899540535457665340073300390626396596970180355253776711660E-18"), // ^23
  276. mp_type("+5.15645517658028233395375998562329055050964428219501277474E-21"), // ^25
  277. mp_type("-1.812399312848887477410034071087545686586497030654642705E-23"), // ^27
  278. mp_type("+5.50728578652238583570585513920522536675023562254864E-26"), // ^29
  279. mp_type("-1.461148710664467988723468673933026649943084902958E-28"), // ^31
  280. mp_type("+3.41405297003316172502972039913417222912445427E-31"), // ^33
  281. mp_type("-7.07885550810745570069916712806856538290251E-34"), // ^35
  282. mp_type("+1.31128947968267628970845439024155655665E-36"), // ^37
  283. mp_type("-2.18318293181145698535113946654065918E-39"), // ^39
  284. mp_type("+3.28462680978498856345937578502923E-42"), // ^41
  285. mp_type("-4.48753699028101089490067137298E-45"), // ^43
  286. mp_type("+5.59219884208696457859353716E-48"), // ^45
  287. mp_type("-6.38214503973500471720565E-51"), // ^47
  288. mp_type("+6.69528558381794452556E-54"), // ^49
  289. mp_type("-6.47841373182350206E-57"), // ^51
  290. mp_type("+5.800016389666445E-60"), // ^53
  291. mp_type("-4.818507347289E-63"), // ^55
  292. mp_type("+3.724683686E-66"), // ^57
  293. mp_type("-2.6856479E-69"), // ^59
  294. mp_type("+1.81046E-72"), // ^61
  295. mp_type("-1.133E-75"), // ^63
  296. }};
  297. const mp_type v = x * 2 / boost::math::constants::pi<mp_type>();
  298. const mp_type x2 = (v * v);
  299. //
  300. // Polynomial evaluation follows, if mp_type allocates memory then
  301. // just one such allocation occurs - to initialize the variable "sum" -
  302. // and no temporaries are created at all.
  303. //
  304. const mp_type sum = ((((((((((((((((((((((((((((((( + coefs[31U]
  305. * x2 + coefs[30U])
  306. * x2 + coefs[29U])
  307. * x2 + coefs[28U])
  308. * x2 + coefs[27U])
  309. * x2 + coefs[26U])
  310. * x2 + coefs[25U])
  311. * x2 + coefs[24U])
  312. * x2 + coefs[23U])
  313. * x2 + coefs[22U])
  314. * x2 + coefs[21U])
  315. * x2 + coefs[20U])
  316. * x2 + coefs[19U])
  317. * x2 + coefs[18U])
  318. * x2 + coefs[17U])
  319. * x2 + coefs[16U])
  320. * x2 + coefs[15U])
  321. * x2 + coefs[14U])
  322. * x2 + coefs[13U])
  323. * x2 + coefs[12U])
  324. * x2 + coefs[11U])
  325. * x2 + coefs[10U])
  326. * x2 + coefs[9U])
  327. * x2 + coefs[8U])
  328. * x2 + coefs[7U])
  329. * x2 + coefs[6U])
  330. * x2 + coefs[5U])
  331. * x2 + coefs[4U])
  332. * x2 + coefs[3U])
  333. * x2 + coefs[2U])
  334. * x2 + coefs[1U])
  335. * x2 + coefs[0U])
  336. * v;
  337. return sum;
  338. }
  339. /*`
  340. Calling the function like so:
  341. mp_type pid4 = boost::math::constants::pi<mp_type>() / 4;
  342. std::cout << std::setprecision(std::numeric_limits< ::mp_type>::digits10) << std::scientific;
  343. std::cout << mysin(pid4) << std::endl;
  344. Yields the expected output:
  345. [pre 7.0710678118654752440084436210484903928483593768847403658833986900e-01]
  346. */
  347. //]
  348. int main()
  349. {
  350. using namespace boost::multiprecision;
  351. std::cout << std::scientific << std::setprecision(std::numeric_limits<double>::digits10);
  352. std::cout << JEL1(2.5, 0.5) << std::endl;
  353. std::cout << std::scientific << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10);
  354. std::cout << JEL2(cpp_dec_float_50(2.5), cpp_dec_float_50(0.5)) << std::endl;
  355. std::cout << std::scientific << std::setprecision(std::numeric_limits<double>::digits10);
  356. std::cout << JEL3(2.5, 0.5) << std::endl;
  357. std::cout << std::scientific << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10);
  358. std::cout << JEL3(cpp_dec_float_50(2.5), cpp_dec_float_50(0.5)) << std::endl;
  359. std::cout << std::scientific << std::setprecision(std::numeric_limits<cpp_dec_float_100>::digits10);
  360. std::cout << JEL4(cpp_dec_float_100(2) + 0.5, cpp_dec_float_100(0.5)) << std::endl;
  361. //[AOS2
  362. /*=#include <iostream>
  363. #include <iomanip>
  364. #include <boost/multiprecision/cpp_dec_float.hpp>
  365. using boost::multiprecision::cpp_dec_float_50;
  366. int main(int, char**)
  367. {*/
  368. const float r_f(float(123) / 100);
  369. const float a_f = area_of_a_circle(r_f);
  370. const double r_d(double(123) / 100);
  371. const double a_d = area_of_a_circle(r_d);
  372. const cpp_dec_float_50 r_mp(cpp_dec_float_50(123) / 100);
  373. const cpp_dec_float_50 a_mp = area_of_a_circle(r_mp);
  374. // 4.75292
  375. std::cout
  376. << std::setprecision(std::numeric_limits<float>::digits10)
  377. << a_f
  378. << std::endl;
  379. // 4.752915525616
  380. std::cout
  381. << std::setprecision(std::numeric_limits<double>::digits10)
  382. << a_d
  383. << std::endl;
  384. // 4.7529155256159981904701331745635599135018975843146
  385. std::cout
  386. << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10)
  387. << a_mp
  388. << std::endl;
  389. /*=}*/
  390. //]
  391. //[ND2
  392. /*=
  393. #include <iostream>
  394. #include <iomanip>
  395. #include <boost/multiprecision/cpp_dec_float.hpp>
  396. #include <boost/math/constants/constants.hpp>
  397. int main(int, char**)
  398. {*/
  399. using boost::math::constants::pi;
  400. using boost::multiprecision::cpp_dec_float_50;
  401. //
  402. // We'll pass a function pointer for the function object passed to derivative,
  403. // the typecast is needed to select the correct overload of std::sin:
  404. //
  405. const float d_f = derivative(
  406. pi<float>() / 3,
  407. 0.01F,
  408. static_cast<float(*)(float)>(std::sin)
  409. );
  410. const double d_d = derivative(
  411. pi<double>() / 3,
  412. 0.001,
  413. static_cast<double(*)(double)>(std::sin)
  414. );
  415. //
  416. // In the cpp_dec_float_50 case, the sin function is multiply overloaded
  417. // to handle expression templates etc. As a result it's hard to take its
  418. // address without knowing about its implementation details. We'll use a
  419. // C++11 lambda expression to capture the call.
  420. // We also need a typecast on the first argument so we don't accidentally pass
  421. // an expression template to a template function:
  422. //
  423. const cpp_dec_float_50 d_mp = derivative(
  424. cpp_dec_float_50(pi<cpp_dec_float_50>() / 3),
  425. cpp_dec_float_50(1.0E-9),
  426. [](const cpp_dec_float_50& x) -> cpp_dec_float_50
  427. {
  428. return sin(x);
  429. }
  430. );
  431. // 5.000029e-001
  432. std::cout
  433. << std::setprecision(std::numeric_limits<float>::digits10)
  434. << d_f
  435. << std::endl;
  436. // 4.999999999998876e-001
  437. std::cout
  438. << std::setprecision(std::numeric_limits<double>::digits10)
  439. << d_d
  440. << std::endl;
  441. // 4.99999999999999999999999999999999999999999999999999e-01
  442. std::cout
  443. << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10)
  444. << d_mp
  445. << std::endl;
  446. //=}
  447. /*`
  448. The expected value of the derivative is 0.5. This central difference rule in this
  449. example is ill-conditioned, meaning it suffers from slight loss of precision. With that
  450. in mind, the results agree with the expected value of 0.5.*/
  451. //]
  452. //[ND3
  453. /*`
  454. We can take this a step further and use our derivative function to compute
  455. a partial derivative. For example if we take the incomplete gamma function
  456. ['P(a, z)], and take the derivative with respect to /z/ at /(2,2)/ then we
  457. can calculate the result as shown below, for good measure we'll compare with
  458. the "correct" result obtained from a call to ['gamma_p_derivative], the results
  459. agree to approximately 44 digits:
  460. */
  461. cpp_dec_float_50 gd = derivative(
  462. cpp_dec_float_50(2),
  463. cpp_dec_float_50(1.0E-9),
  464. [](const cpp_dec_float_50& x) ->cpp_dec_float_50
  465. {
  466. return boost::math::gamma_p(2, x);
  467. }
  468. );
  469. // 2.70670566473225383787998989944968806815263091819151e-01
  470. std::cout
  471. << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10)
  472. << gd
  473. << std::endl;
  474. // 2.70670566473225383787998989944968806815253190143120e-01
  475. std::cout << boost::math::gamma_p_derivative(cpp_dec_float_50(2), cpp_dec_float_50(2)) << std::endl;
  476. //]
  477. //[GI2
  478. /* The function can now be called as follows: */
  479. /*=int main(int, char**)
  480. {*/
  481. using boost::math::constants::pi;
  482. typedef boost::multiprecision::cpp_dec_float_50 mp_type;
  483. const float j2_f =
  484. integral(0.0F,
  485. pi<float>(),
  486. 0.01F,
  487. cyl_bessel_j_integral_rep<float>(2U, 1.23F)) / pi<float>();
  488. const double j2_d =
  489. integral(0.0,
  490. pi<double>(),
  491. 0.0001,
  492. cyl_bessel_j_integral_rep<double>(2U, 1.23)) / pi<double>();
  493. const mp_type j2_mp =
  494. integral(mp_type(0),
  495. pi<mp_type>(),
  496. mp_type(1.0E-20),
  497. cyl_bessel_j_integral_rep<mp_type>(2U, mp_type(123) / 100)) / pi<mp_type>();
  498. // 0.166369
  499. std::cout
  500. << std::setprecision(std::numeric_limits<float>::digits10)
  501. << j2_f
  502. << std::endl;
  503. // 0.166369383786814
  504. std::cout
  505. << std::setprecision(std::numeric_limits<double>::digits10)
  506. << j2_d
  507. << std::endl;
  508. // 0.16636938378681407351267852431513159437103348245333
  509. std::cout
  510. << std::setprecision(std::numeric_limits<mp_type>::digits10)
  511. << j2_mp
  512. << std::endl;
  513. //
  514. // Print true value for comparison:
  515. // 0.166369383786814073512678524315131594371033482453329
  516. std::cout << boost::math::cyl_bessel_j(2, mp_type(123) / 100) << std::endl;
  517. //=}
  518. //]
  519. std::cout << std::setprecision(std::numeric_limits< ::mp_type>::digits10) << std::scientific;
  520. std::cout << mysin(boost::math::constants::pi< ::mp_type>() / 4) << std::endl;
  521. std::cout << boost::multiprecision::sin(boost::math::constants::pi< ::mp_type>() / 4) << std::endl;
  522. return 0;
  523. }
  524. /*
  525. Program output:
  526. 9.822663964796047e-001
  527. 9.82266396479604757017335009796882833995903762577173e-01
  528. 9.822663964796047e-001
  529. 9.82266396479604757017335009796882833995903762577173e-01
  530. 9.8226639647960475701733500979688283399590376257717309069410413822165082248153638454147004236848917775e-01
  531. 4.752916e+000
  532. 4.752915525615998e+000
  533. 4.75291552561599819047013317456355991350189758431460e+00
  534. 5.000029e-001
  535. 4.999999999998876e-001
  536. 4.99999999999999999999999999999999999999999999999999e-01
  537. 2.70670566473225383787998989944968806815263091819151e-01
  538. 2.70670566473225383787998989944968806815253190143120e-01
  539. 7.0710678118654752440084436210484903928483593768847403658833986900e-01
  540. 7.0710678118654752440084436210484903928483593768847403658833986900e-01
  541. */
  542. #else
  543. int main() { return 0; }
  544. #endif