2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Fix example so it actually compiles, add better OutputIterator usage and fix try/catch blocks.

This commit is contained in:
jzmaddock
2014-01-09 10:18:49 +00:00
parent 1706a3c2b6
commit 2216a07483

View File

@@ -12,7 +12,7 @@
//#define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
#include <boost/math/concepts/real_concept.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/math/special_functions/bernoulli.hpp>
#include <iostream>
@@ -57,20 +57,18 @@ and also obtain much higher precision.
*/
std::cout
<< std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10)
<< boost::math::bernoulli_b2n<cpp_dec_float_50>(100) << std::endl;
<< std::setprecision(std::numeric_limits<boost::multiprecision::cpp_dec_float_50>::digits10)
<< boost::math::bernoulli_b2n<boost::multiprecision::cpp_dec_float_50>(100) << std::endl;
//] //[/bernoulli_example_1]
//[bernoulli_example_2
/*`We can compute and save all the float-precision Bernoulli numbers from one call.
*/
std::vector<float> bn(32); // Space for all the 32-bit `float` precision Bernoulli numbers.
std::vector<float>::iterator it = bn.begin();
std::vector<float> bn; // Space for all the 32-bit `float` precision Bernoulli numbers.
// Start with Bernoulli number 0.
boost::math::bernoulli_b2n<float>(0, bn.size(), it); // Fill vector with even Bernoulli numbers.
boost::math::bernoulli_b2n<float>(0, 32, std::back_inserter(bn)); // Fill vector with even Bernoulli numbers.
for(size_t i = 0; i < bn.size(); i++)
{ // Show vector of even Bernoulli numbers, showing all significant decimal digits.
@@ -81,17 +79,28 @@ and also obtain much higher precision.
}
//] //[/bernoulli_example_2]
}
catch(const std::exception&)
{
}
//[bernoulli_example_3
/*`Of course, for any floating-point type, there is a maximum Bernoulli number than can be computed
before it overflows the exponent.
If we try to compute too high a Bernoulli number, an exception will be thrown,
*/
try{
std::cout
<< std::setprecision(std::numeric_limits<float>::digits10)
<< "Bernoulli number " << 33 * 2 <<std::endl;
std::cout << boost::math::bernoulli_b2n<float>(33) << std::endl;
}
catch (std::exception ex)
{
std::cout << "Thrown Exception caught: " << ex.what() << std::endl;
}
/*`
and (provided 'try'n'catch' blocks are used) we will get a helpful error message.
@@ -99,11 +108,6 @@ and (provided 'try'n'catch' blocks are used) we will get a helpful error message
//] //[/bernoulli_example_3]
}
catch (std::exception ex)
{
std::cout << "Thrown Exception caught: " << ex.what() << std::endl;
}
} // int main()
@@ -151,9 +155,8 @@ and (provided 'try'n'catch' blocks are used) we will get a helpful error message
//] //[/bernoulli_output_2]
//[bernoulli_output_3
Bernoulli number 66
Thrown Exception caught: Error in function boost::math::bernoulli<float>: Overflow error while calculating tangent number 2
Bernoulli number 66
Thrown Exception caught: Error in function boost::math::bernoulli_b2n<float>(n): Overflow evaluating function at 33
//] //[/bernoulli_output_3]
*/