policy_eg_7.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_7
  12. #include <boost/math/distributions.hpp> // All distributions.
  13. // using boost::math::normal; // Would create an ambguity between
  14. // boost::math::normal_distribution<RealType> boost::math::normal and
  15. // 'anonymous-namespace'::normal'.
  16. namespace
  17. { // anonymous or unnnamed (rather than named as in policy_eg_6.cpp).
  18. using namespace boost::math::policies;
  19. // using boost::math::policies::errno_on_error; // etc.
  20. typedef policy<
  21. // return infinity and set errno rather than throw:
  22. overflow_error<errno_on_error>,
  23. // Don't promote double -> long double internally:
  24. promote_double<false>,
  25. // Return the closest integer result for discrete quantiles:
  26. discrete_quantile<integer_round_nearest>
  27. > my_policy;
  28. BOOST_MATH_DECLARE_DISTRIBUTIONS(double, my_policy)
  29. } // close namespace my_namespace
  30. int main()
  31. {
  32. // Construct distribution with something we know will overflow.
  33. normal norm(10, 2); // using 'anonymous-namespace'::normal
  34. errno = 0;
  35. cout << "Result of quantile(norm, 0) is: "
  36. << quantile(norm, 0) << endl;
  37. cout << "errno = " << errno << endl;
  38. errno = 0;
  39. cout << "Result of quantile(norm, 1) is: "
  40. << quantile(norm, 1) << endl;
  41. cout << "errno = " << errno << endl;
  42. //
  43. // Now try a discrete distribution:
  44. binomial binom(20, 0.25);
  45. cout << "Result of quantile(binom, 0.05) is: "
  46. << quantile(binom, 0.05) << endl;
  47. cout << "Result of quantile(complement(binom, 0.05)) is: "
  48. << quantile(complement(binom, 0.05)) << endl;
  49. }
  50. //] //[/policy_eg_7]
  51. /*
  52. Output:
  53. Result of quantile(norm, 0) is: -1.#INF
  54. errno = 34
  55. Result of quantile(norm, 1) is: 1.#INF
  56. errno = 34
  57. Result of quantile(binom, 0.05) is: 1
  58. Result of quantile(complement(binom, 0.05)) is: 8
  59. */