1 // Copyright John Maddock 2006, 2007.
2 // Copyright Paul A. Bristow 2006, 2007, 2012.
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP
10 #define BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP
12 #include <boost/math/policies/error_handling.hpp>
13 #include <boost/math/special_functions/fpclassify.hpp>
14 // using boost::math::isfinite;
15 // using boost::math::isnan;
17 namespace boost{ namespace math{ namespace detail
20 template <class RealType, class Policy>
21 inline bool check_probability(const char* function, RealType const& prob, RealType* result, const Policy& pol)
23 if((prob < 0) || (prob > 1) || !(boost::math::isfinite)(prob))
25 *result = policies::raise_domain_error<RealType>(
27 "Probability argument is %1%, but must be >= 0 and <= 1 !", prob, pol);
33 template <class RealType, class Policy>
34 inline bool check_df(const char* function, RealType const& df, RealType* result, const Policy& pol)
35 { // df > 0 but NOT +infinity allowed.
36 if((df <= 0) || !(boost::math::isfinite)(df))
38 *result = policies::raise_domain_error<RealType>(
40 "Degrees of freedom argument is %1%, but must be > 0 !", df, pol);
46 template <class RealType, class Policy>
47 inline bool check_df_gt0_to_inf(const char* function, RealType const& df, RealType* result, const Policy& pol)
48 { // df > 0 or +infinity are allowed.
49 if( (df <= 0) || (boost::math::isnan)(df) )
50 { // is bad df <= 0 or NaN or -infinity.
51 *result = policies::raise_domain_error<RealType>(
53 "Degrees of freedom argument is %1%, but must be > 0 !", df, pol);
57 } // check_df_gt0_to_inf
60 template <class RealType, class Policy>
61 inline bool check_scale(
67 if((scale <= 0) || !(boost::math::isfinite)(scale))
68 { // Assume scale == 0 is NOT valid for any distribution.
69 *result = policies::raise_domain_error<RealType>(
71 "Scale parameter is %1%, but must be > 0 !", scale, pol);
77 template <class RealType, class Policy>
78 inline bool check_location(
84 if(!(boost::math::isfinite)(location))
86 *result = policies::raise_domain_error<RealType>(
88 "Location parameter is %1%, but must be finite!", location, pol);
94 template <class RealType, class Policy>
101 // Note that this test catches both infinity and NaN.
102 // Some distributions permit x to be infinite, so these must be tested 1st and return,
103 // leaving this test to catch any NaNs.
104 // See Normal, Logistic and Cauchy for example.
105 if(!(boost::math::isfinite)(x))
107 *result = policies::raise_domain_error<RealType>(
109 "Random variate x is %1%, but must be finite!", x, pol);
115 template <class RealType, class Policy>
116 inline bool check_x_gt0(
117 const char* function,
124 *result = policies::raise_domain_error<RealType>(
126 "Random variate x is %1%, but must be > 0!", x, pol);
131 // Note that this test catches both infinity and NaN.
132 // Some special cases permit x to be infinite, so these must be tested 1st,
133 // leaving this test to catch any NaNs. See Normal and cauchy for example.
134 } // bool check_x_gt0
136 template <class RealType, class Policy>
137 inline bool check_positive_x(
138 const char* function,
143 if(!(boost::math::isfinite)(x) || (x < 0))
145 *result = policies::raise_domain_error<RealType>(
147 "Random variate x is %1%, but must be finite and >= 0!", x, pol);
151 // Note that this test catches both infinity and NaN.
152 // Some special cases permit x to be infinite, so these must be tested 1st,
153 // leaving this test to catch any NaNs. see Normal and cauchy for example.
156 template <class RealType, class Policy>
157 inline bool check_non_centrality(
158 const char* function,
163 if((ncp < 0) || !(boost::math::isfinite)(ncp))
164 { // Assume scale == 0 is NOT valid for any distribution.
165 *result = policies::raise_domain_error<RealType>(
167 "Non centrality parameter is %1%, but must be > 0 !", ncp, pol);
173 template <class RealType, class Policy>
174 inline bool check_finite(
175 const char* function,
180 if(!(boost::math::isfinite)(x))
181 { // Assume scale == 0 is NOT valid for any distribution.
182 *result = policies::raise_domain_error<RealType>(
184 "Parameter is %1%, but must be finite !", x, pol);
190 } // namespace detail
194 #endif // BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP