policy_eg_1.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include <iostream>
  7. using std::cout; using std::endl;
  8. #include <cerrno> // for ::errno
  9. //[policy_eg_1
  10. #include <boost/math/special_functions/gamma.hpp>
  11. using boost::math::tgamma;
  12. // Define the policy to use:
  13. using namespace boost::math::policies; // may be convenient, or
  14. using boost::math::policies::policy;
  15. // Types of error whose action can be altered by policies:.
  16. using boost::math::policies::evaluation_error;
  17. using boost::math::policies::domain_error;
  18. using boost::math::policies::overflow_error;
  19. using boost::math::policies::domain_error;
  20. using boost::math::policies::pole_error;
  21. // Actions on error (in enum error_policy_type):
  22. using boost::math::policies::errno_on_error;
  23. using boost::math::policies::ignore_error;
  24. using boost::math::policies::throw_on_error;
  25. using boost::math::policies::user_error;
  26. typedef policy<
  27. domain_error<errno_on_error>,
  28. pole_error<errno_on_error>,
  29. overflow_error<errno_on_error>,
  30. evaluation_error<errno_on_error>
  31. > c_policy;
  32. //
  33. // Now use the policy when calling tgamma:
  34. // http://msdn.microsoft.com/en-us/library/t3ayayh1.aspx
  35. // Microsoft errno declared in STDLIB.H as "extern int errno;"
  36. int main()
  37. {
  38. errno = 0; // Reset.
  39. cout << "Result of tgamma(30000) is: "
  40. << tgamma(30000, c_policy()) << endl; // Too big parameter
  41. cout << "errno = " << errno << endl; // errno 34 Numerical result out of range.
  42. cout << "Result of tgamma(-10) is: "
  43. << boost::math::tgamma(-10, c_policy()) << endl; // Negative parameter.
  44. cout << "errno = " << errno << endl; // error 33 Numerical argument out of domain.
  45. } // int main()
  46. //]
  47. /* Output
  48. policy_eg_1.cpp
  49. Generating code
  50. Finished generating code
  51. policy_eg_1.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\policy_eg_1.exe
  52. Result of tgamma(30000) is: 1.#INF
  53. errno = 34
  54. Result of tgamma(-10) is: 1.#QNAN
  55. errno = 33
  56. */