1 // Copyright John Maddock 2007. 2 // Copyright Paul A. Bristow 2010 3 // Use, modification and distribution are subject to the 4 // Boost Software License, Version 1.0. (See accompanying file 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 7 // Note that this file contains quickbook mark-up as well as code 8 // and comments, don't change any of the special comment mark-ups! 9 10 #include <boost/config.hpp> 11 #ifdef _MSC_VER 12 # pragma warning (disable : 4189) // 'd' : local variable is initialized but not referenced 13 #endif 14 #ifdef BOOST_GCC 15 # pragma GCC diagnostic ignored "-Wunused-variable" 16 #endif 17 18 #include <iostream> 19 using std::cout; using std::endl; 20 21 #include <stdexcept> 22 using std::domain_error; 23 24 //[policy_ref_snip13 25 26 #include <boost/math/distributions/cauchy.hpp> 27 28 namespace myspace 29 { // using namespace boost::math::policies; // May be convenient in myspace. 30 31 // Define a policy called my_policy to use. 32 using boost::math::policies::policy; 33 34 // In this case we want all the distribution accessor functions to compile, 35 // even if they are mathematically undefined, so 36 // make the policy assert_undefined. 37 using boost::math::policies::assert_undefined; 38 39 typedef policy<assert_undefined<false> > my_policy; 40 41 // Finally apply this policy to type double. 42 BOOST_MATH_DECLARE_DISTRIBUTIONS(double, my_policy) 43 } // namespace myspace 44 45 // Now we can use myspace::cauchy etc, which will use policy 46 // myspace::mypolicy: 47 // 48 // This compiles but throws a domain error exception at runtime. 49 // Caution! If you omit the try'n'catch blocks, 50 // it will just silently terminate, giving no clues as to why! 51 // So try'n'catch blocks are very strongly recommended. 52 test_cauchy()53void test_cauchy() 54 { 55 try 56 { 57 double d = mean(myspace::cauchy()); // Cauchy does not have a mean! 58 (void) d; 59 } 60 catch(const std::domain_error& e) 61 { 62 cout << e.what() << endl; 63 } 64 } 65 66 //] //[/policy_ref_snip13] 67 main()68int main() 69 { 70 test_cauchy(); 71 } 72 73 /* 74 75 Output: 76 77 policy_snip_13.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\policy_snip_13.exe 78 Error in function boost::math::mean(cauchy<double>&): The Cauchy distribution does not have a mean: the only possible return value is 1.#QNAN. 79 80 */ 81 82