policy_eg_6.cpp 3.4 KB

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