policy_ref_snip13.cpp 2.2 KB

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