bernoulli_example.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright Paul A. Bristow 2013.
  2. // Copyright Nakhar Agrawal 2013.
  3. // Copyright John Maddock 2013.
  4. // Copyright Christopher Kormanyos 2013.
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #pragma warning (disable : 4100) // unreferenced formal parameter.
  9. #pragma warning (disable : 4127) // conditional expression is constant.
  10. //#define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
  11. #include <boost/multiprecision/cpp_dec_float.hpp>
  12. #include <boost/math/special_functions/bernoulli.hpp>
  13. #include <iostream>
  14. /* First 50 from 2 to 100 inclusive: */
  15. /* TABLE[N[BernoulliB[n], 200], {n,2,100,2}] */
  16. //SC_(0.1666666666666666666666666666666666666666),
  17. //SC_(-0.0333333333333333333333333333333333333333),
  18. //SC_(0.0238095238095238095238095238095238095238),
  19. //SC_(-0.0333333333333333333333333333333333333333),
  20. //SC_(0.0757575757575757575757575757575757575757),
  21. //SC_(-0.2531135531135531135531135531135531135531),
  22. //SC_(1.1666666666666666666666666666666666666666),
  23. //SC_(-7.0921568627450980392156862745098039215686),
  24. //SC_(54.9711779448621553884711779448621553884711),
  25. int main()
  26. {
  27. //[bernoulli_example_1
  28. /*`A simple example computes the value of B[sub 4] where the return type is `double`,
  29. note that the argument to bernoulli_b2n is ['2] not ['4] since it computes B[sub 2N].
  30. */
  31. try
  32. { // It is always wise to use try'n'catch blocks around Boost.Math functions
  33. // so that any informative error messages can be displayed in the catch block.
  34. std::cout
  35. << std::setprecision(std::numeric_limits<double>::digits10)
  36. << boost::math::bernoulli_b2n<double>(2) << std::endl;
  37. /*`So B[sub 4] == -1/30 == -0.0333333333333333
  38. If we use Boost.Multiprecision and its 50 decimal digit floating-point type `cpp_dec_float_50`,
  39. we can calculate the value of much larger numbers like B[sub 200]
  40. and also obtain much higher precision.
  41. */
  42. std::cout
  43. << std::setprecision(std::numeric_limits<boost::multiprecision::cpp_dec_float_50>::digits10)
  44. << boost::math::bernoulli_b2n<boost::multiprecision::cpp_dec_float_50>(100) << std::endl;
  45. //] //[/bernoulli_example_1]
  46. //[bernoulli_example_2
  47. /*`We can compute and save all the float-precision Bernoulli numbers from one call.
  48. */
  49. std::vector<float> bn; // Space for 32-bit `float` precision Bernoulli numbers.
  50. // Start with Bernoulli number 0.
  51. boost::math::bernoulli_b2n<float>(0, 32, std::back_inserter(bn)); // Fill vector with even Bernoulli numbers.
  52. for(size_t i = 0; i < bn.size(); i++)
  53. { // Show vector of even Bernoulli numbers, showing all significant decimal digits.
  54. std::cout << std::setprecision(std::numeric_limits<float>::digits10)
  55. << i*2 << ' '
  56. << bn[i]
  57. << std::endl;
  58. }
  59. //] //[/bernoulli_example_2]
  60. }
  61. catch(const std::exception& ex)
  62. {
  63. std::cout << "Thrown Exception caught: " << ex.what() << std::endl;
  64. }
  65. //[bernoulli_example_3
  66. /*`Of course, for any floating-point type, there is a maximum Bernoulli number that can be computed
  67. before it overflows the exponent.
  68. By default policy, if we try to compute too high a Bernoulli number, an exception will be thrown.
  69. */
  70. try
  71. {
  72. std::cout
  73. << std::setprecision(std::numeric_limits<float>::digits10)
  74. << "Bernoulli number " << 33 * 2 <<std::endl;
  75. std::cout << boost::math::bernoulli_b2n<float>(33) << std::endl;
  76. }
  77. catch (std::exception ex)
  78. {
  79. std::cout << "Thrown Exception caught: " << ex.what() << std::endl;
  80. }
  81. /*`
  82. and we will get a helpful error message (provided try'n'catch blocks are used).
  83. */
  84. //] //[/bernoulli_example_3]
  85. //[bernoulli_example_4
  86. /*For example:
  87. */
  88. std::cout << "boost::math::max_bernoulli_b2n<float>::value = " << boost::math::max_bernoulli_b2n<float>::value << std::endl;
  89. std::cout << "Maximum Bernoulli number using float is " << boost::math::bernoulli_b2n<float>( boost::math::max_bernoulli_b2n<float>::value) << std::endl;
  90. std::cout << "boost::math::max_bernoulli_b2n<double>::value = " << boost::math::max_bernoulli_b2n<double>::value << std::endl;
  91. std::cout << "Maximum Bernoulli number using double is " << boost::math::bernoulli_b2n<double>( boost::math::max_bernoulli_b2n<double>::value) << std::endl;
  92. //] //[/bernoulli_example_4]
  93. //[tangent_example_1
  94. /*`We can compute and save a few Tangent numbers.
  95. */
  96. std::vector<float> tn; // Space for some `float` precision Tangent numbers.
  97. // Start with Bernoulli number 0.
  98. boost::math::tangent_t2n<float>(1, 6, std::back_inserter(tn)); // Fill vector with even Tangent numbers.
  99. for(size_t i = 0; i < tn.size(); i++)
  100. { // Show vector of even Tangent numbers, showing all significant decimal digits.
  101. std::cout << std::setprecision(std::numeric_limits<float>::digits10)
  102. << " "
  103. << tn[i];
  104. }
  105. std::cout << std::endl;
  106. //] [/tangent_example_1]
  107. // 1, 2, 16, 272, 7936, 353792, 22368256, 1903757312
  108. } // int main()
  109. /*
  110. //[bernoulli_output_1
  111. -3.6470772645191354362138308865549944904868234686191e+215
  112. //] //[/bernoulli_output_1]
  113. //[bernoulli_output_2
  114. 0 1
  115. 2 0.166667
  116. 4 -0.0333333
  117. 6 0.0238095
  118. 8 -0.0333333
  119. 10 0.0757576
  120. 12 -0.253114
  121. 14 1.16667
  122. 16 -7.09216
  123. 18 54.9712
  124. 20 -529.124
  125. 22 6192.12
  126. 24 -86580.3
  127. 26 1.42552e+006
  128. 28 -2.72982e+007
  129. 30 6.01581e+008
  130. 32 -1.51163e+010
  131. 34 4.29615e+011
  132. 36 -1.37117e+013
  133. 38 4.88332e+014
  134. 40 -1.92966e+016
  135. 42 8.41693e+017
  136. 44 -4.03381e+019
  137. 46 2.11507e+021
  138. 48 -1.20866e+023
  139. 50 7.50087e+024
  140. 52 -5.03878e+026
  141. 54 3.65288e+028
  142. 56 -2.84988e+030
  143. 58 2.38654e+032
  144. 60 -2.14e+034
  145. 62 2.0501e+036
  146. //] //[/bernoulli_output_2]
  147. //[bernoulli_output_3
  148. Bernoulli number 66
  149. Thrown Exception caught: Error in function boost::math::bernoulli_b2n<float>(n):
  150. Overflow evaluating function at 33
  151. //] //[/bernoulli_output_3]
  152. //[bernoulli_output_4
  153. boost::math::max_bernoulli_b2n<float>::value = 32
  154. Maximum Bernoulli number using float is -2.0938e+038
  155. boost::math::max_bernoulli_b2n<double>::value = 129
  156. Maximum Bernoulli number using double is 1.33528e+306
  157. //] //[/bernoulli_output_4]
  158. //[tangent_output_1
  159. 1 2 16 272 7936 353792
  160. //] [/tangent_output_1]
  161. */