1 // C_error_policy_example.cpp
2
3 // Copyright Paul A. Bristow 2007, 2010.
4 // Copyright John Maddock 2007.
5
6 // Use, modification and distribution are subject to the
7 // Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt
9 // or copy at http://www.boost.org/LICENSE_1_0.txt)
10
11 // Suppose we want a call to tgamma to behave in a C-compatible way
12 // and set global ::errno rather than throw an exception.
13
14 #include <cerrno> // for ::errno
15
16 #include <boost/math/special_functions/gamma.hpp>
17 using boost::math::tgamma;
18
19 using boost::math::policies::policy;
20 // Possible errors
21 using boost::math::policies::overflow_error;
22 using boost::math::policies::underflow_error;
23 using boost::math::policies::domain_error;
24 using boost::math::policies::pole_error;
25 using boost::math::policies::denorm_error;
26 using boost::math::policies::evaluation_error;
27
28 using boost::math::policies::errno_on_error;
29 using boost::math::policies::ignore_error;
30
31 //using namespace boost::math::policies;
32 //using namespace boost::math; // avoid potential ambiguity with std:: <random>
33
34 // Define a policy:
35 typedef policy<
36 domain_error<errno_on_error>, // 'bad' arguments.
37 pole_error<errno_on_error>, // argument is pole value.
38 overflow_error<errno_on_error>, // argument value causes overflow.
39 evaluation_error<errno_on_error> // evaluation does not converge and may be inaccurate, or worse,
40 // or there is no way known (yet) to implement this evaluation,
41 // for example, kurtosis of non-central beta distribution.
42 > C_error_policy;
43
44 // std
45 #include <iostream>
46 using std::cout;
47 using std::endl;
48
main()49 int main()
50 {
51 // We can achieve this at the function call site
52 // with the previously defined policy C_error_policy.
53 double t = tgamma(4., C_error_policy());
54 cout << "tgamma(4., C_error_policy() = " << t << endl; // 6
55
56 // Alternatively we could use the function make_policy,
57 // provided for convenience,
58 // and define everything at the call site:
59 t = tgamma(4., make_policy(
60 domain_error<errno_on_error>(),
61 pole_error<errno_on_error>(),
62 overflow_error<errno_on_error>(),
63 evaluation_error<errno_on_error>()
64 ));
65 cout << "tgamma(4., make_policy(...) = " << t << endl; // 6
66
67 return 0;
68 } // int main()
69
70 /*
71
72 Output
73
74 c_error_policy_example.cpp
75 Generating code
76 Finished generating code
77 c_error_policy_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\c_error_policy_example.exe
78 tgamma(4., C_error_policy() = 6
79 tgamma(4., make_policy(...) = 6
80 tgamma(4., C_error_policy() = 6
81 tgamma(4., make_policy(...) = 6
82
83 */
84