]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/policy_eg_6.cpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / libs / math / example / policy_eg_6.cpp
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
7 // Note that this file contains quickbook mark-up as well as code
8 // and comments, don't change any of the special comment mark-ups!
9
10 #include <iostream>
11 using std::cout; using std::endl;
12 #include <cerrno> // for ::errno
13
14 //[policy_eg_6
15
16 /*`
17 Suppose we want a set of distributions to behave as follows:
18
19 * Return infinity on overflow, rather than throwing an exception.
20 * Don't perform any promotion from double to long double internally.
21 * Return the closest integer result from the quantiles of discrete
22 distributions.
23
24 We'll begin by including the needed header for all the distributions:
25 */
26
27 #include <boost/math/distributions.hpp>
28
29 /*`
30
31 Open up an appropriate namespace, calling it `my_distributions`,
32 for our distributions, and define the policy type we want.
33 Any policies we don't specify here will inherit the defaults:
34
35 */
36
37 namespace my_distributions
38 {
39 using namespace boost::math::policies;
40 // using boost::math::policies::errno_on_error; // etc.
41
42 typedef policy<
43 // return infinity and set errno rather than throw:
44 overflow_error<errno_on_error>,
45 // Don't promote double -> long double internally:
46 promote_double<false>,
47 // Return the closest integer result for discrete quantiles:
48 discrete_quantile<integer_round_nearest>
49 > my_policy;
50
51 /*`
52
53 All we need do now is invoke the BOOST_MATH_DECLARE_DISTRIBUTIONS
54 macro passing the floating point type `double` and policy types `my_policy` as arguments:
55
56 */
57
58 BOOST_MATH_DECLARE_DISTRIBUTIONS(double, my_policy)
59
60 } // close namespace my_namespace
61
62 /*`
63
64 We now have a set of typedefs defined in namespace my_distributions
65 that all look something like this:
66
67 ``
68 typedef boost::math::normal_distribution<double, my_policy> normal;
69 typedef boost::math::cauchy_distribution<double, my_policy> cauchy;
70 typedef boost::math::gamma_distribution<double, my_policy> gamma;
71 // etc
72 ``
73
74 So that when we use my_distributions::normal we really end up using
75 `boost::math::normal_distribution<double, my_policy>`:
76
77 */
78
79 int main()
80 {
81 // Construct distribution with something we know will overflow
82 // (using double rather than if promoted to long double):
83 my_distributions::normal norm(10, 2);
84
85 errno = 0;
86 cout << "Result of quantile(norm, 0) is: "
87 << quantile(norm, 0) << endl; // -infinity.
88 cout << "errno = " << errno << endl;
89 errno = 0;
90 cout << "Result of quantile(norm, 1) is: "
91 << quantile(norm, 1) << endl; // +infinity.
92 cout << "errno = " << errno << endl;
93
94 // Now try a discrete distribution.
95 my_distributions::binomial binom(20, 0.25);
96 cout << "Result of quantile(binom, 0.05) is: "
97 << quantile(binom, 0.05) << endl; // To check we get integer results.
98 cout << "Result of quantile(complement(binom, 0.05)) is: "
99 << quantile(complement(binom, 0.05)) << endl;
100 }
101
102 /*`
103
104 Which outputs:
105
106 [pre
107 Result of quantile(norm, 0) is: -1.#INF
108 errno = 34
109 Result of quantile(norm, 1) is: 1.#INF
110 errno = 34
111 Result of quantile(binom, 0.05) is: 1
112 Result of quantile(complement(binom, 0.05)) is: 8
113 ]
114
115 This mechanism is particularly useful when we want to define a
116 project-wide policy, and don't want to modify the Boost source
117 or set project wide build macros (possibly fragile and easy to forget).
118
119 */
120 //] //[/policy_eg_6]
121