policy_eg_5.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <iostream>
  9. using std::cout; using std::endl;
  10. #include <cerrno> // for ::errno
  11. //[policy_eg_5
  12. #include <boost/math/special_functions.hpp>
  13. // using boost::math::tgamma; // Would create an ambiguity between
  14. // 'double boost::math::tgamma<int>(T)' and
  15. // 'double 'anonymous-namespace'::tgamma<int>(RT)'.
  16. namespace mymath
  17. { // unnamed
  18. using namespace boost::math::policies;
  19. typedef policy<
  20. domain_error<errno_on_error>,
  21. pole_error<errno_on_error>,
  22. overflow_error<errno_on_error>,
  23. evaluation_error<errno_on_error>
  24. > c_policy;
  25. BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS(c_policy)
  26. /*`
  27. So that when we call `mymath::tgamma(z)`, we really end up calling
  28. `boost::math::tgamma(z, anonymous-namespace::c_policy())`.
  29. */
  30. } // close unnamed namespace
  31. int main()
  32. {
  33. errno = 0;
  34. cout << "Result of tgamma(30000) is: "
  35. << mymath::tgamma(30000) << endl;
  36. // tgamma in unnamed namespace in this translation unit (file) only.
  37. cout << "errno = " << errno << endl;
  38. cout << "Result of tgamma(-10) is: "
  39. << mymath::tgamma(-10) << endl;
  40. cout << "errno = " << errno << endl;
  41. // Default tgamma policy would throw an exception, and abort.
  42. }
  43. //] //[/policy_eg_5]
  44. /*
  45. Output:
  46. Result of tgamma(30000) is: 1.#INF
  47. errno = 34
  48. Result of tgamma(-10) is: 1.#QNAN
  49. errno = 33
  50. */