error_policy_example.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // example_policy_handling.cpp
  2. // Copyright Paul A. Bristow 2007, 2010.
  3. // Copyright John Maddock 2007.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. // See error_handling_example.cpp for use of
  9. // macro definition to change policy for
  10. // domain_error - negative degrees of freedom argument
  11. // for student's t distribution CDF,
  12. // and catching the exception.
  13. // See error_handling_policies.cpp for more examples.
  14. // Boost
  15. #include <boost/math/distributions/students_t.hpp>
  16. using boost::math::students_t_distribution; // Probability of students_t(df, t).
  17. using boost::math::students_t; // Probability of students_t(df, t) convenience typedef for double.
  18. using boost::math::policies::policy;
  19. using boost::math::policies::domain_error;
  20. using boost::math::policies::ignore_error;
  21. // std
  22. #include <iostream>
  23. using std::cout;
  24. using std::endl;
  25. #include <stdexcept>
  26. // Define a (bad?) policy to ignore domain errors ('bad' arguments):
  27. typedef policy<
  28. domain_error<ignore_error>
  29. > my_policy;
  30. // Define my_students_t distribution with this different domain error policy:
  31. typedef students_t_distribution<double, my_policy> my_students_t;
  32. int main()
  33. { // Example of error handling of bad argument(s) to a distribution.
  34. cout << "Example error handling using Student's t function. " << endl;
  35. double degrees_of_freedom = -1; double t = -1.; // Two 'bad' arguments!
  36. try
  37. {
  38. cout << "Probability of ignore_error Student's t is "
  39. << cdf(my_students_t(degrees_of_freedom), t) << endl;
  40. cout << "Probability of default error policy Student's t is " << endl;
  41. // By contrast the students_t distribution default domain error policy is to throw,
  42. cout << cdf(students_t(-1), -1) << endl; // so this will throw.
  43. /*`
  44. Message from thrown exception was:
  45. Error in function boost::math::students_t_distribution<double>::students_t_distribution:
  46. Degrees of freedom argument is -1, but must be > 0 !
  47. */
  48. // We could also define a 'custom' distribution
  49. // with an "ignore overflow error policy" in a single statement:
  50. using boost::math::policies::overflow_error;
  51. students_t_distribution<double, policy<overflow_error<ignore_error> > > students_t_no_throw(-1);
  52. }
  53. catch(const std::exception& e)
  54. {
  55. std::cout <<
  56. "\n""Message from thrown exception was:\n " << e.what() << std::endl;
  57. }
  58. return 0;
  59. } // int main()
  60. /*
  61. Output:
  62. error_policy_example.cpp
  63. Generating code
  64. Finished generating code
  65. error_policy_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\error_policy_example.exe
  66. Example error handling using Student's t function.
  67. Probability of ignore_error Student's t is 1.#QNAN
  68. Probability of default error policy Student's t is
  69. Message from thrown exception was:
  70. Error in function boost::math::students_t_distribution<double>::students_t_distribution: Degrees of freedom argument is -1, but must be > 0 !
  71. */