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