]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // error_policies_example.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 | #include <boost/math/distributions/normal.hpp> | |
12 | using boost::math::normal_distribution; | |
13 | ||
14 | #include <boost/math/distributions/students_t.hpp> | |
15 | using boost::math::students_t; // Probability of students_t(df, t). | |
16 | using boost::math::students_t_distribution; | |
17 | ||
18 | // using namespace boost::math; causes: | |
19 | //.\error_policy_normal.cpp(30) : error C2872: 'policy' : ambiguous symbol | |
20 | // could be '\boost/math/policies/policy.hpp(392) : boost::math::policies::policy' | |
21 | // or 'boost::math::policies' | |
22 | ||
23 | // So should not use this 'using namespace boost::math;' command. | |
24 | ||
25 | // Suppose we want a statistical distribution to return infinities, | |
26 | // rather than throw exceptions (the default policy), then we can use: | |
27 | ||
28 | // std | |
29 | #include <iostream> | |
30 | using std::cout; | |
31 | using std::endl; | |
32 | ||
33 | // using namespace boost::math::policies; or | |
34 | ||
35 | using boost::math::policies::policy; | |
36 | // Possible errors | |
37 | using boost::math::policies::overflow_error; | |
38 | using boost::math::policies::underflow_error; | |
39 | using boost::math::policies::domain_error; | |
40 | using boost::math::policies::pole_error; | |
41 | using boost::math::policies::denorm_error; | |
42 | using boost::math::policies::evaluation_error; | |
43 | using boost::math::policies::ignore_error; | |
44 | ||
45 | // Define a custom policy to ignore just overflow: | |
46 | typedef policy< | |
47 | overflow_error<ignore_error> | |
48 | > my_policy; | |
49 | ||
50 | // Define another custom policy (perhaps ill-advised?) | |
51 | // to ignore all errors: domain, pole, overflow, underflow, denorm & evaluation: | |
52 | typedef policy< | |
53 | domain_error<ignore_error>, | |
54 | pole_error<ignore_error>, | |
55 | overflow_error<ignore_error>, | |
56 | underflow_error<ignore_error>, | |
57 | denorm_error<ignore_error>, | |
58 | evaluation_error<ignore_error> | |
59 | > my_ignoreall_policy; | |
60 | ||
61 | // Define a new distribution with a custom policy to ignore_error | |
62 | // (& thus perhaps return infinity for some arguments): | |
63 | typedef boost::math::normal_distribution<double, my_policy> my_normal; | |
64 | // Note: uses default parameters zero mean and unit standard deviation. | |
65 | ||
66 | // We could also do the same for another distribution, for example: | |
67 | using boost::math::students_t_distribution; | |
68 | typedef students_t_distribution<double, my_ignoreall_policy> my_students_t; | |
69 | ||
70 | int main() | |
71 | { | |
72 | cout << "quantile(my_normal(), 0.05); = " << quantile(my_normal(), 0.05) << endl; // 0.05 is argument within normal range. | |
73 | cout << "quantile(my_normal(), 0.); = " << quantile(my_normal(), 0.) << endl; // argument zero, so expect infinity. | |
74 | cout << "quantile(my_normal(), 0.); = " << quantile(my_normal(), 0.F) << endl; // argument zero, so expect infinity. | |
75 | ||
76 | cout << "quantile(my_students_t(), 0.); = " << quantile(my_students_t(-1), 0.F) << endl; // 'bad' argument negative, so expect NaN. | |
77 | ||
78 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
79 | // Construct a (0, 1) normal distribution that ignores all errors, | |
80 | // returning NaN, infinity, zero, or best guess, | |
81 | // and NOT setting errno. | |
82 | normal_distribution<long double, my_ignoreall_policy> my_normal2(0.L, 1.L); // explicit parameters for distribution. | |
83 | cout << "quantile(my_normal2(), 0.); = " << quantile(my_normal2, 0.01) << endl; // argument 0.01, so result finite. | |
84 | cout << "quantile(my_normal2(), 0.); = " << quantile(my_normal2, 0.) << endl; // argument zero, so expect infinity. | |
85 | #endif | |
86 | ||
87 | return 0; | |
88 | } | |
89 | ||
90 | /* | |
91 | ||
92 | Output: | |
93 | ||
94 | error_policies_example.cpp | |
95 | Generating code | |
96 | Finished generating code | |
97 | error_policy_normal_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\error_policies_example.exe | |
98 | quantile(my_normal(), 0.05); = -1.64485 | |
99 | quantile(my_normal(), 0.); = -1.#INF | |
100 | quantile(my_normal(), 0.); = -1.#INF | |
101 | quantile(my_students_t(), 0.); = 1.#QNAN | |
102 | quantile(my_normal2(), 0.); = -2.32635 | |
103 | quantile(my_normal2(), 0.); = -1.#INF | |
104 | ||
105 | */ |