error_handling_example.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // example_error_handling.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. // Note that this file contains quickbook markup as well as code
  9. // and comments, don't change any of the special comment markups!
  10. // Optional macro definitions described in text below:
  11. // #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error
  12. // #define BOOST_MATH_DOMAIN_ERROR_POLICY errno_on_error
  13. // #define BOOST_MATH_DOMAIN_ERROR_POLICY is set to: throw_on_error
  14. //[error_handling_example
  15. /*`
  16. The following example demonstrates the effect of
  17. setting the macro BOOST_MATH_DOMAIN_ERROR_POLICY
  18. when an invalid argument is encountered. For the
  19. purposes of this example, we'll pass a negative
  20. degrees of freedom parameter to the student's t
  21. distribution.
  22. Since we know that this is a single file program we could
  23. just add:
  24. #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error
  25. to the top of the source file to change the default policy
  26. to one that simply returns a NaN when a domain error occurs.
  27. Alternatively we could use:
  28. #define BOOST_MATH_DOMAIN_ERROR_POLICY errno_on_error
  29. To ensure the `::errno` is set when a domain error occurs
  30. as well as returning a NaN.
  31. This is safe provided the program consists of a single
  32. translation unit /and/ we place the define /before/ any
  33. #includes. Note that should we add the define after the includes
  34. then it will have no effect! A warning such as:
  35. [pre warning C4005: 'BOOST_MATH_OVERFLOW_ERROR_POLICY' : macro redefinition]
  36. is a certain sign that it will /not/ have the desired effect.
  37. We'll begin our sample program with the needed includes:
  38. */
  39. #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error
  40. // Boost
  41. #include <boost/math/distributions/students_t.hpp>
  42. using boost::math::students_t; // Probability of students_t(df, t).
  43. // std
  44. #include <iostream>
  45. using std::cout;
  46. using std::endl;
  47. #include <stdexcept>
  48. #include <cstddef>
  49. // using ::errno
  50. /*`
  51. Next we'll define the program's main() to call the student's t
  52. distribution with an invalid degrees of freedom parameter,
  53. the program is set up to handle either an exception or a NaN:
  54. */
  55. int main()
  56. {
  57. cout << "Example error handling using Student's t function. " << endl;
  58. cout << "BOOST_MATH_DOMAIN_ERROR_POLICY is set to: "
  59. << BOOST_STRINGIZE(BOOST_MATH_DOMAIN_ERROR_POLICY) << endl;
  60. double degrees_of_freedom = -1; // A bad argument!
  61. double t = 10;
  62. try
  63. {
  64. errno = 0; // Clear/reset.
  65. students_t dist(degrees_of_freedom); // exception is thrown here if enabled.
  66. double p = cdf(dist, t);
  67. // Test for error reported by other means:
  68. if((boost::math::isnan)(p))
  69. {
  70. cout << "cdf returned a NaN!" << endl;
  71. if (errno != 0)
  72. { // So errno has been set.
  73. cout << "errno is set to: " << errno << endl;
  74. }
  75. }
  76. else
  77. cout << "Probability of Student's t is " << p << endl;
  78. }
  79. catch(const std::exception& e)
  80. {
  81. std::cout <<
  82. "\n""Message from thrown exception was:\n " << e.what() << std::endl;
  83. }
  84. return 0;
  85. } // int main()
  86. /*`
  87. Here's what the program output looks like with a default build
  88. (one that *does throw exceptions*):
  89. [pre
  90. Example error handling using Student's t function.
  91. BOOST_MATH_DOMAIN_ERROR_POLICY is set to: throw_on_error
  92. Message from thrown exception was:
  93. Error in function boost::math::students_t_distribution<double>::students_t_distribution:
  94. Degrees of freedom argument is -1, but must be > 0 !
  95. ]
  96. Alternatively let's build with:
  97. #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error
  98. Now the program output is:
  99. [pre
  100. Example error handling using Student's t function.
  101. BOOST_MATH_DOMAIN_ERROR_POLICY is set to: ignore_error
  102. cdf returned a NaN!
  103. ]
  104. And finally let's build with:
  105. #define BOOST_MATH_DOMAIN_ERROR_POLICY errno_on_error
  106. Which gives the output show errno:
  107. [pre
  108. Example error handling using Student's t function.
  109. BOOST_MATH_DOMAIN_ERROR_POLICY is set to: errno_on_error
  110. cdf returned a NaN!
  111. errno is set to: 33
  112. ]
  113. */
  114. //] [error_handling_eg end quickbook markup]