• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 // Note that this file contains quickbook mark-up as well as code
8 // and comments, don't change any of the special comment mark-ups!
9 
10 //[policy_ref_snip1
11 
12 #include <boost/math/special_functions/gamma.hpp>
13 using boost::math::tgamma;
14 
15 //using namespace boost::math::policies; may also be convenient.
16 using boost::math::policies::policy;
17 using boost::math::policies::evaluation_error;
18 using boost::math::policies::domain_error;
19 using boost::math::policies::overflow_error;
20 using boost::math::policies::domain_error;
21 using boost::math::policies::pole_error;
22 using boost::math::policies::errno_on_error;
23 
24 // Define a policy:
25 typedef policy<
26   domain_error<errno_on_error>,
27   pole_error<errno_on_error>,
28   overflow_error<errno_on_error>,
29   evaluation_error<errno_on_error>
30 > my_policy;
31 
32 double my_value = 0.; //
33 
34 // Call the function applying my_policy:
35 double t1 = tgamma(my_value, my_policy());
36 
37 // Alternatively (and equivalently) we could use helpful function
38 // make_policy and define everything at the call site:
39 double t2 = tgamma(my_value,
40   make_policy(
41     domain_error<errno_on_error>(),
42     pole_error<errno_on_error>(),
43     overflow_error<errno_on_error>(),
44     evaluation_error<errno_on_error>() )
45   );
46 //]
47 
48 #include <iostream>
49 using std::cout; using std::endl;
50 
main()51 int main()
52 {
53   cout << "my_value = " << my_value << endl;
54   try
55   { // First with default policy - throw an exception.
56      cout << "tgamma(my_value) = " << tgamma(my_value) << endl;
57   }
58   catch(const std::exception& e)
59   {
60      cout <<"\n""Message from thrown exception was:\n   " << e.what() << endl;
61   }
62 
63   cout << "tgamma(my_value, my_policy() = " << t1 << endl;
64   cout << "tgamma(my_value, make_policy(domain_error<errno_on_error>(), pole_error<errno_on_error>(), overflow_error<errno_on_error>(), evaluation_error<errno_on_error>() ) = " << t2 << endl;
65 }
66 
67 /*
68 Output:
69   my_value = 0
70 
71   Message from thrown exception was:
72      Error in function boost::math::tgamma<long double>(long double): Evaluation of tgamma at a negative integer 0.
73   tgamma(my_value, my_policy() = 1.#QNAN
74   tgamma(my_value, make_policy(domain_error<errno_on_error>(), pole_error<errno_on_error>(), overflow_error<errno_on_error>(), evaluation_error<errno_on_error>() ) = 1.#QNAN
75 */
76