c_error_policy_example.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // C_error_policy_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. // Suppose we want a call to tgamma to behave in a C-compatible way
  9. // and set global ::errno rather than throw an exception.
  10. #include <cerrno> // for ::errno
  11. #include <boost/math/special_functions/gamma.hpp>
  12. using boost::math::tgamma;
  13. using boost::math::policies::policy;
  14. // Possible errors
  15. using boost::math::policies::overflow_error;
  16. using boost::math::policies::underflow_error;
  17. using boost::math::policies::domain_error;
  18. using boost::math::policies::pole_error;
  19. using boost::math::policies::denorm_error;
  20. using boost::math::policies::evaluation_error;
  21. using boost::math::policies::errno_on_error;
  22. using boost::math::policies::ignore_error;
  23. //using namespace boost::math::policies;
  24. //using namespace boost::math; // avoid potential ambiuity with std:: <random>
  25. // Define a policy:
  26. typedef policy<
  27. domain_error<errno_on_error>, // 'bad' arguments.
  28. pole_error<errno_on_error>, // argument is pole value.
  29. overflow_error<errno_on_error>, // argument value causes overflow.
  30. evaluation_error<errno_on_error> // evaluation does not converge and may be inaccurate, or worse,
  31. // or there is no way known (yet) to implement this evaluation,
  32. // for example, kurtosis of non-central beta distribution.
  33. > C_error_policy;
  34. // std
  35. #include <iostream>
  36. using std::cout;
  37. using std::endl;
  38. int main()
  39. {
  40. // We can achieve this at the function call site
  41. // with the previously defined policy C_error_policy.
  42. double t = tgamma(4., C_error_policy());
  43. cout << "tgamma(4., C_error_policy() = " << t << endl; // 6
  44. // Alternatively we could use the function make_policy,
  45. // provided for convenience,
  46. // and define everything at the call site:
  47. t = tgamma(4., make_policy(
  48. domain_error<errno_on_error>(),
  49. pole_error<errno_on_error>(),
  50. overflow_error<errno_on_error>(),
  51. evaluation_error<errno_on_error>()
  52. ));
  53. cout << "tgamma(4., make_policy(...) = " << t << endl; // 6
  54. return 0;
  55. } // int main()
  56. /*
  57. Output
  58. c_error_policy_example.cpp
  59. Generating code
  60. Finished generating code
  61. c_error_policy_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\c_error_policy_example.exe
  62. tgamma(4., C_error_policy() = 6
  63. tgamma(4., make_policy(...) = 6
  64. tgamma(4., C_error_policy() = 6
  65. tgamma(4., make_policy(...) = 6
  66. */