policy_ref_snip12.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. // Define tgamma function with a no overflow policy
  9. // into a specific namespace-scope.
  10. #include <iostream>
  11. using std::cout; using std::endl;
  12. //[policy_ref_snip12
  13. #include <boost/math/special_functions/gamma.hpp>
  14. //using boost::math::tgamma;
  15. // Need not declare using boost::math::tgamma here,
  16. // because will define tgamma in myspace using macro below.
  17. namespace myspace
  18. {
  19. using namespace boost::math::policies;
  20. // Define a policy that does not throw on overflow:
  21. typedef policy<overflow_error<errno_on_error> > my_policy;
  22. // Define the special functions in this scope to use the policy:
  23. BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS(my_policy)
  24. }
  25. // Now we can use myspace::tgamma etc.
  26. // They will automatically use "my_policy":
  27. //
  28. double t = myspace::tgamma(30.0); // Will *not* throw on overflow,
  29. // despite the large value of factorial 30 = 265252859812191058636308480000000
  30. // unlike default policy boost::math::tgamma;
  31. //]
  32. int main()
  33. {
  34. cout << "myspace::tgamma(30.0) = " << t << endl;
  35. }
  36. /*
  37. Output:
  38. myspace::tgamma(30.0) = 8.84176e+030
  39. */