From e0737c56c1e384e05064a0c6e1ac46979371041b Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Wed, 19 Dec 2018 22:58:32 +0100 Subject: New upstream version 2.0.0+repack --- boost/libs/math/example/policy_eg_6.cpp | 121 ++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 boost/libs/math/example/policy_eg_6.cpp (limited to 'boost/libs/math/example/policy_eg_6.cpp') diff --git a/boost/libs/math/example/policy_eg_6.cpp b/boost/libs/math/example/policy_eg_6.cpp new file mode 100644 index 00000000..57f92fcc --- /dev/null +++ b/boost/libs/math/example/policy_eg_6.cpp @@ -0,0 +1,121 @@ +// Copyright John Maddock 2007. +// Copyright Paul A. Bristow 2010 +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Note that this file contains quickbook mark-up as well as code +// and comments, don't change any of the special comment mark-ups! + +#include +using std::cout; using std::endl; +#include // for ::errno + +//[policy_eg_6 + +/*` +Suppose we want a set of distributions to behave as follows: + +* Return infinity on overflow, rather than throwing an exception. +* Don't perform any promotion from double to long double internally. +* Return the closest integer result from the quantiles of discrete +distributions. + +We'll begin by including the needed header for all the distributions: +*/ + +#include + +/*` + +Open up an appropriate namespace, calling it `my_distributions`, +for our distributions, and define the policy type we want. +Any policies we don't specify here will inherit the defaults: + +*/ + +namespace my_distributions +{ + using namespace boost::math::policies; + // using boost::math::policies::errno_on_error; // etc. + + typedef policy< + // return infinity and set errno rather than throw: + overflow_error, + // Don't promote double -> long double internally: + promote_double, + // Return the closest integer result for discrete quantiles: + discrete_quantile + > my_policy; + +/*` + +All we need do now is invoke the BOOST_MATH_DECLARE_DISTRIBUTIONS +macro passing the floating point type `double` and policy types `my_policy` as arguments: + +*/ + +BOOST_MATH_DECLARE_DISTRIBUTIONS(double, my_policy) + +} // close namespace my_namespace + +/*` + +We now have a set of typedefs defined in namespace my_distributions +that all look something like this: + +`` +typedef boost::math::normal_distribution normal; +typedef boost::math::cauchy_distribution cauchy; +typedef boost::math::gamma_distribution gamma; +// etc +`` + +So that when we use my_distributions::normal we really end up using +`boost::math::normal_distribution`: + +*/ + +int main() +{ + // Construct distribution with something we know will overflow + // (using double rather than if promoted to long double): + my_distributions::normal norm(10, 2); + + errno = 0; + cout << "Result of quantile(norm, 0) is: " + << quantile(norm, 0) << endl; // -infinity. + cout << "errno = " << errno << endl; + errno = 0; + cout << "Result of quantile(norm, 1) is: " + << quantile(norm, 1) << endl; // +infinity. + cout << "errno = " << errno << endl; + + // Now try a discrete distribution. + my_distributions::binomial binom(20, 0.25); + cout << "Result of quantile(binom, 0.05) is: " + << quantile(binom, 0.05) << endl; // To check we get integer results. + cout << "Result of quantile(complement(binom, 0.05)) is: " + << quantile(complement(binom, 0.05)) << endl; +} + +/*` + +Which outputs: + +[pre +Result of quantile(norm, 0) is: -1.#INF +errno = 34 +Result of quantile(norm, 1) is: 1.#INF +errno = 34 +Result of quantile(binom, 0.05) is: 1 +Result of quantile(complement(binom, 0.05)) is: 8 +] + +This mechanism is particularly useful when we want to define a +project-wide policy, and don't want to modify the Boost source +or set project wide build macros (possibly fragile and easy to forget). + +*/ +//] //[/policy_eg_6] + -- cgit v1.2.3