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