| 1 |
// Copyright John Maddock 2006, 2007. |
| 2 |
// Copyright Paul A. Bristow 2006, 2007, 2012. |
| 3 |
// Copyright Matt Borland 2024 |
| 4 |
|
| 5 |
// Use, modification and distribution are subject to the |
| 6 |
// Boost Software License, Version 1.0. |
| 7 |
// (See accompanying file LICENSE_1_0.txt |
| 8 |
// or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 9 |
|
| 10 |
#ifndef BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP |
| 11 |
#define BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP |
| 12 |
|
| 13 |
#include <boost/math/tools/config.hpp> |
| 14 |
#include <boost/math/tools/numeric_limits.hpp> |
| 15 |
#include <boost/math/policies/error_handling.hpp> |
| 16 |
#include <boost/math/special_functions/fpclassify.hpp> |
| 17 |
// using boost::math::isfinite; |
| 18 |
// using boost::math::isnan; |
| 19 |
|
| 20 |
#ifdef _MSC_VER |
| 21 |
# pragma warning(push) |
| 22 |
# pragma warning(disable: 4702) // unreachable code (return after domain_error throw). |
| 23 |
#endif |
| 24 |
|
| 25 |
namespace boost{ namespace math{ namespace detail |
| 26 |
{ |
| 27 |
|
| 28 |
template <class RealType, class Policy> |
| 29 |
BOOST_MATH_GPU_ENABLED inline bool check_probability(const char* function, RealType const& prob, RealType* result, const Policy& pol) |
| 30 |
{ |
| 31 |
if((prob < 0) || (prob > 1) || !(boost::math::isfinite)(prob)) |
| 32 |
{ |
| 33 |
*result = policies::raise_domain_error<RealType>( |
| 34 |
function, |
| 35 |
"Probability argument is %1%, but must be >= 0 and <= 1 !", prob, pol); |
| 36 |
return false; |
| 37 |
} |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
template <class RealType, class Policy> |
| 42 |
BOOST_MATH_GPU_ENABLED inline bool check_df(const char* function, RealType const& df, RealType* result, const Policy& pol) |
| 43 |
{ // df > 0 but NOT +infinity allowed. |
| 44 |
if((df <= 0) || !(boost::math::isfinite)(df)) |
| 45 |
{ |
| 46 |
*result = policies::raise_domain_error<RealType>( |
| 47 |
function, |
| 48 |
"Degrees of freedom argument is %1%, but must be > 0 !", df, pol); |
| 49 |
return false; |
| 50 |
} |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
template <class RealType, class Policy> |
| 55 |
BOOST_MATH_GPU_ENABLED inline bool check_df_gt0_to_inf(const char* function, RealType const& df, RealType* result, const Policy& pol) |
| 56 |
{ // df > 0 or +infinity are allowed. |
| 57 |
if( (df <= 0) || (boost::math::isnan)(df) ) |
| 58 |
{ // is bad df <= 0 or NaN or -infinity. |
| 59 |
*result = policies::raise_domain_error<RealType>( |
| 60 |
function, |
| 61 |
"Degrees of freedom argument is %1%, but must be > 0 !", df, pol); |
| 62 |
return false; |
| 63 |
} |
| 64 |
return true; |
| 65 |
} // check_df_gt0_to_inf |
| 66 |
|
| 67 |
|
| 68 |
template <class RealType, class Policy> |
| 69 |
BOOST_MATH_GPU_ENABLED inline bool check_scale( |
| 70 |
const char* function, |
| 71 |
RealType scale, |
| 72 |
RealType* result, |
| 73 |
const Policy& pol) |
| 74 |
{ |
| 75 |
if((scale <= 0) || !(boost::math::isfinite)(scale)) |
| 76 |
{ // Assume scale == 0 is NOT valid for any distribution. |
| 77 |
*result = policies::raise_domain_error<RealType>( |
| 78 |
function, |
| 79 |
"Scale parameter is %1%, but must be > 0 !", scale, pol); |
| 80 |
return false; |
| 81 |
} |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
template <class RealType, class Policy> |
| 86 |
BOOST_MATH_GPU_ENABLED inline bool check_location( |
| 87 |
const char* function, |
| 88 |
RealType location, |
| 89 |
RealType* result, |
| 90 |
const Policy& pol) |
| 91 |
{ |
| 92 |
if(!(boost::math::isfinite)(location)) |
| 93 |
{ |
| 94 |
*result = policies::raise_domain_error<RealType>( |
| 95 |
function, |
| 96 |
"Location parameter is %1%, but must be finite!", location, pol); |
| 97 |
return false; |
| 98 |
} |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
template <class RealType, class Policy> |
| 103 |
BOOST_MATH_GPU_ENABLED inline bool check_x( |
| 104 |
const char* function, |
| 105 |
RealType x, |
| 106 |
RealType* result, |
| 107 |
const Policy& pol) |
| 108 |
{ |
| 109 |
// Note that this test catches both infinity and NaN. |
| 110 |
// Some distributions permit x to be infinite, so these must be tested 1st and return, |
| 111 |
// leaving this test to catch any NaNs. |
| 112 |
// See Normal, Logistic, Laplace and Cauchy for example. |
| 113 |
if(!(boost::math::isfinite)(x)) |
| 114 |
{ |
| 115 |
*result = policies::raise_domain_error<RealType>( |
| 116 |
function, |
| 117 |
"Random variate x is %1%, but must be finite!", x, pol); |
| 118 |
return false; |
| 119 |
} |
| 120 |
return true; |
| 121 |
} // bool check_x |
| 122 |
|
| 123 |
template <class RealType, class Policy> |
| 124 |
BOOST_MATH_GPU_ENABLED inline bool check_x_not_NaN( |
| 125 |
const char* function, |
| 126 |
RealType x, |
| 127 |
RealType* result, |
| 128 |
const Policy& pol) |
| 129 |
{ |
| 130 |
// Note that this test catches only NaN. |
| 131 |
// Some distributions permit x to be infinite, leaving this test to catch any NaNs. |
| 132 |
// See Normal, Logistic, Laplace and Cauchy for example. |
| 133 |
if ((boost::math::isnan)(x)) |
| 134 |
{ |
| 135 |
*result = policies::raise_domain_error<RealType>( |
| 136 |
function, |
| 137 |
"Random variate x is %1%, but must be finite or + or - infinity!", x, pol); |
| 138 |
return false; |
| 139 |
} |
| 140 |
return true; |
| 141 |
} // bool check_x_not_NaN |
| 142 |
|
| 143 |
template <class RealType, class Policy> |
| 144 |
BOOST_MATH_GPU_ENABLED inline bool check_x_gt0( |
| 145 |
const char* function, |
| 146 |
RealType x, |
| 147 |
RealType* result, |
| 148 |
const Policy& pol) |
| 149 |
{ |
| 150 |
if(x <= 0) |
| 151 |
{ |
| 152 |
*result = policies::raise_domain_error<RealType>( |
| 153 |
function, |
| 154 |
"Random variate x is %1%, but must be > 0!", x, pol); |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
return true; |
| 159 |
// Note that this test catches both infinity and NaN. |
| 160 |
// Some special cases permit x to be infinite, so these must be tested 1st, |
| 161 |
// leaving this test to catch any NaNs. See Normal and cauchy for example. |
| 162 |
} // bool check_x_gt0 |
| 163 |
|
| 164 |
template <class RealType, class Policy> |
| 165 |
BOOST_MATH_GPU_ENABLED inline bool check_positive_x( |
| 166 |
const char* function, |
| 167 |
RealType x, |
| 168 |
RealType* result, |
| 169 |
const Policy& pol) |
| 170 |
{ |
| 171 |
if(!(boost::math::isfinite)(x) || (x < 0)) |
| 172 |
{ |
| 173 |
*result = policies::raise_domain_error<RealType>( |
| 174 |
function, |
| 175 |
"Random variate x is %1%, but must be finite and >= 0!", x, pol); |
| 176 |
return false; |
| 177 |
} |
| 178 |
return true; |
| 179 |
// Note that this test catches both infinity and NaN. |
| 180 |
// Some special cases permit x to be infinite, so these must be tested 1st, |
| 181 |
// leaving this test to catch any NaNs. see Normal and cauchy for example. |
| 182 |
} |
| 183 |
|
| 184 |
template <class RealType, class Policy> |
| 185 |
BOOST_MATH_GPU_ENABLED inline bool check_non_centrality( |
| 186 |
const char* function, |
| 187 |
RealType ncp, |
| 188 |
RealType* result, |
| 189 |
const Policy& pol) |
| 190 |
{ |
| 191 |
BOOST_MATH_STATIC const RealType upper_limit = static_cast<RealType>((boost::math::numeric_limits<long long>::max)()) - boost::math::policies::get_max_root_iterations<Policy>(); |
| 192 |
|
| 193 |
if((ncp < 0) || !(boost::math::isfinite)(ncp) || ncp > upper_limit) |
| 194 |
{ |
| 195 |
*result = policies::raise_domain_error<RealType>( |
| 196 |
function, |
| 197 |
"Non centrality parameter is %1%, but must be > 0, and a countable value such that x+1 != x", ncp, pol); |
| 198 |
return false; |
| 199 |
} |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
template <class RealType, class Policy> |
| 204 |
BOOST_MATH_GPU_ENABLED inline bool check_finite( |
| 205 |
const char* function, |
| 206 |
RealType x, |
| 207 |
RealType* result, |
| 208 |
const Policy& pol) |
| 209 |
{ |
| 210 |
if(!(boost::math::isfinite)(x)) |
| 211 |
{ // Assume scale == 0 is NOT valid for any distribution. |
| 212 |
*result = policies::raise_domain_error<RealType>( |
| 213 |
function, |
| 214 |
"Parameter is %1%, but must be finite !", x, pol); |
| 215 |
return false; |
| 216 |
} |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
} // namespace detail |
| 221 |
} // namespace math |
| 222 |
} // namespace boost |
| 223 |
|
| 224 |
#ifdef _MSC_VER |
| 225 |
# pragma warning(pop) |
| 226 |
#endif |
| 227 |
|
| 228 |
#endif // BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP |