]>
git.proxmox.com Git - ceph.git/blob - 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)
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!
11 using std::cout
; using std::endl
;
12 #include <cerrno> // for ::errno
17 Suppose we want a set of distributions to behave as follows:
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
24 We'll begin by including the needed header for all the distributions:
27 #include <boost/math/distributions.hpp>
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:
37 namespace my_distributions
39 using namespace boost::math::policies
;
40 // using boost::math::policies::errno_on_error; // etc.
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
>
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:
58 BOOST_MATH_DECLARE_DISTRIBUTIONS(double, my_policy
)
60 } // close namespace my_namespace
64 We now have a set of typedefs defined in namespace my_distributions
65 that all look something like this:
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;
74 So that when we use my_distributions::normal we really end up using
75 `boost::math::normal_distribution<double, my_policy>`:
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);
86 cout
<< "Result of quantile(norm, 0) is: "
87 << quantile(norm
, 0) << endl
; // -infinity.
88 cout
<< "errno = " << errno
<< endl
;
90 cout
<< "Result of quantile(norm, 1) is: "
91 << quantile(norm
, 1) << endl
; // +infinity.
92 cout
<< "errno = " << errno
<< endl
;
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
;
107 Result of quantile(norm, 0) is: -1.#INF
109 Result of quantile(norm, 1) is: 1.#INF
111 Result of quantile(binom, 0.05) is: 1
112 Result of quantile(complement(binom, 0.05)) is: 8
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).