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