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