]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/error_policy_example.cpp
1 // example_policy_handling.cpp
3 // Copyright Paul A. Bristow 2007, 2010.
4 // Copyright John Maddock 2007.
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)
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.
17 // See error_handling_policies.cpp for more examples.
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.
24 using boost::math::policies::policy
;
25 using boost::math::policies::domain_error
;
26 using boost::math::policies::ignore_error
;
36 // Define a (bad?) policy to ignore domain errors ('bad' arguments):
38 domain_error
<ignore_error
>
41 // Define my_students_t distribution with this different domain error policy:
42 typedef students_t_distribution
<double, my_policy
> my_students_t
;
45 { // Example of error handling of bad argument(s) to a distribution.
46 cout
<< "Example error handling using Student's t function. " << endl
;
48 double degrees_of_freedom
= -1; double t
= -1.; // Two 'bad' arguments!
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.
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 !
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);
69 catch(const std::exception
& e
)
72 "\n""Message from thrown exception was:\n " << e
.what() << std::endl
;
82 error_policy_example.cpp
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
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 !