policy_ref_snip1.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. //[policy_ref_snip1
  9. #include <boost/math/special_functions/gamma.hpp>
  10. using boost::math::tgamma;
  11. //using namespace boost::math::policies; may also be convenient.
  12. using boost::math::policies::policy;
  13. using boost::math::policies::evaluation_error;
  14. using boost::math::policies::domain_error;
  15. using boost::math::policies::overflow_error;
  16. using boost::math::policies::domain_error;
  17. using boost::math::policies::pole_error;
  18. using boost::math::policies::errno_on_error;
  19. // Define a policy:
  20. typedef policy<
  21. domain_error<errno_on_error>,
  22. pole_error<errno_on_error>,
  23. overflow_error<errno_on_error>,
  24. evaluation_error<errno_on_error>
  25. > my_policy;
  26. double my_value = 0.; //
  27. // Call the function applying my_policy:
  28. double t1 = tgamma(my_value, my_policy());
  29. // Alternatively (and equivalently) we could use helpful function
  30. // make_policy and define everything at the call site:
  31. double t2 = tgamma(my_value,
  32. make_policy(
  33. domain_error<errno_on_error>(),
  34. pole_error<errno_on_error>(),
  35. overflow_error<errno_on_error>(),
  36. evaluation_error<errno_on_error>() )
  37. );
  38. //]
  39. #include <iostream>
  40. using std::cout; using std::endl;
  41. int main()
  42. {
  43. cout << "my_value = " << my_value << endl;
  44. try
  45. { // First with default policy - throw an exception.
  46. cout << "tgamma(my_value) = " << tgamma(my_value) << endl;
  47. }
  48. catch(const std::exception& e)
  49. {
  50. cout <<"\n""Message from thrown exception was:\n " << e.what() << endl;
  51. }
  52. cout << "tgamma(my_value, my_policy() = " << t1 << endl;
  53. cout << "tgamma(my_value, make_policy(domain_error<errno_on_error>(), pole_error<errno_on_error>(), overflow_error<errno_on_error>(), evaluation_error<errno_on_error>() ) = " << t2 << endl;
  54. }
  55. /*
  56. Output:
  57. my_value = 0
  58. Message from thrown exception was:
  59. Error in function boost::math::tgamma<long double>(long double): Evaluation of tgamma at a negative integer 0.
  60. tgamma(my_value, my_policy() = 1.#QNAN
  61. tgamma(my_value, make_policy(domain_error<errno_on_error>(), pole_error<errno_on_error>(), overflow_error<errno_on_error>(), evaluation_error<errno_on_error>() ) = 1.#QNAN
  62. */