error_policies_example.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // error_policies_example.cpp
  2. // Copyright Paul A. Bristow 2007, 2010.
  3. // Copyright John Maddock 2007.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #include <boost/math/distributions/normal.hpp>
  9. using boost::math::normal_distribution;
  10. #include <boost/math/distributions/students_t.hpp>
  11. using boost::math::students_t; // Probability of students_t(df, t).
  12. using boost::math::students_t_distribution;
  13. // using namespace boost::math; causes:
  14. //.\error_policy_normal.cpp(30) : error C2872: 'policy' : ambiguous symbol
  15. // could be '\boost/math/policies/policy.hpp(392) : boost::math::policies::policy'
  16. // or 'boost::math::policies'
  17. // So should not use this 'using namespace boost::math;' command.
  18. // Suppose we want a statistical distribution to return infinities,
  19. // rather than throw exceptions (the default policy), then we can use:
  20. // std
  21. #include <iostream>
  22. using std::cout;
  23. using std::endl;
  24. // using namespace boost::math::policies; or
  25. using boost::math::policies::policy;
  26. // Possible errors
  27. using boost::math::policies::overflow_error;
  28. using boost::math::policies::underflow_error;
  29. using boost::math::policies::domain_error;
  30. using boost::math::policies::pole_error;
  31. using boost::math::policies::denorm_error;
  32. using boost::math::policies::evaluation_error;
  33. using boost::math::policies::ignore_error;
  34. // Define a custom policy to ignore just overflow:
  35. typedef policy<
  36. overflow_error<ignore_error>
  37. > my_policy;
  38. // Define another custom policy (perhaps ill-advised?)
  39. // to ignore all errors: domain, pole, overflow, underflow, denorm & evaluation:
  40. typedef policy<
  41. domain_error<ignore_error>,
  42. pole_error<ignore_error>,
  43. overflow_error<ignore_error>,
  44. underflow_error<ignore_error>,
  45. denorm_error<ignore_error>,
  46. evaluation_error<ignore_error>
  47. > my_ignoreall_policy;
  48. // Define a new distribution with a custom policy to ignore_error
  49. // (& thus perhaps return infinity for some arguments):
  50. typedef boost::math::normal_distribution<double, my_policy> my_normal;
  51. // Note: uses default parameters zero mean and unit standard deviation.
  52. // We could also do the same for another distribution, for example:
  53. using boost::math::students_t_distribution;
  54. typedef students_t_distribution<double, my_ignoreall_policy> my_students_t;
  55. int main()
  56. {
  57. cout << "quantile(my_normal(), 0.05); = " << quantile(my_normal(), 0.05) << endl; // 0.05 is argument within normal range.
  58. cout << "quantile(my_normal(), 0.); = " << quantile(my_normal(), 0.) << endl; // argument zero, so expect infinity.
  59. cout << "quantile(my_normal(), 0.); = " << quantile(my_normal(), 0.F) << endl; // argument zero, so expect infinity.
  60. cout << "quantile(my_students_t(), 0.); = " << quantile(my_students_t(-1), 0.F) << endl; // 'bad' argument negative, so expect NaN.
  61. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  62. // Construct a (0, 1) normal distribution that ignores all errors,
  63. // returning NaN, infinity, zero, or best guess,
  64. // and NOT setting errno.
  65. normal_distribution<long double, my_ignoreall_policy> my_normal2(0.L, 1.L); // explicit parameters for distribution.
  66. cout << "quantile(my_normal2(), 0.); = " << quantile(my_normal2, 0.01) << endl; // argument 0.01, so result finite.
  67. cout << "quantile(my_normal2(), 0.); = " << quantile(my_normal2, 0.) << endl; // argument zero, so expect infinity.
  68. #endif
  69. return 0;
  70. }
  71. /*
  72. Output:
  73. error_policies_example.cpp
  74. Generating code
  75. Finished generating code
  76. error_policy_normal_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\error_policies_example.exe
  77. quantile(my_normal(), 0.05); = -1.64485
  78. quantile(my_normal(), 0.); = -1.#INF
  79. quantile(my_normal(), 0.); = -1.#INF
  80. quantile(my_students_t(), 0.); = 1.#QNAN
  81. quantile(my_normal2(), 0.); = -2.32635
  82. quantile(my_normal2(), 0.); = -1.#INF
  83. */