2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Update to use new out-of-bounds error checking.

Refs #6934.

[SVN r78771]
This commit is contained in:
John Maddock
2012-05-30 16:52:18 +00:00
parent c732b6e85b
commit 41bd4bd442
11 changed files with 40 additions and 41 deletions

View File

@@ -28,11 +28,11 @@ namespace detail
template <class RealType, class Policy>
inline bool verify_sigma(const char* function, RealType sigma, RealType* presult, const Policy& pol)
{
if(sigma <= 0)
if((sigma <= 0) || (!(boost::math::isfinite)(sigma)))
{
*presult = policies::raise_domain_error<RealType>(
function,
"The scale parameter \"sigma\" must be > 0, but was: %1%.", sigma, pol);
"The scale parameter \"sigma\" must be > 0 and finite, but was: %1%.", sigma, pol);
return false;
}
return true;
@@ -41,7 +41,7 @@ namespace detail
template <class RealType, class Policy>
inline bool verify_rayleigh_x(const char* function, RealType x, RealType* presult, const Policy& pol)
{
if(x < 0)
if((x < 0) || (boost::math::isnan)(x))
{
*presult = policies::raise_domain_error<RealType>(
function,
@@ -81,7 +81,7 @@ template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const rayleigh_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
using boost::math::tools::max_value;
return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
}
template <class RealType, class Policy>
@@ -108,6 +108,10 @@ inline RealType pdf(const rayleigh_distribution<RealType, Policy>& dist, const R
{
return result;
}
if((boost::math::isinf)(x))
{
return 0;
}
RealType sigmasqr = sigma * sigma;
result = x * (exp(-(x * x) / ( 2 * sigmasqr))) / sigmasqr;
return result;

View File

@@ -100,7 +100,9 @@ namespace boost{ namespace math{
inline const std::pair<RealType, RealType> range(const skew_normal_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
using boost::math::tools::max_value;
return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
return std::pair<RealType, RealType>(
std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>()); // - to + max value.
}
template <class RealType, class Policy>

View File

@@ -87,6 +87,9 @@ inline RealType pdf(const students_t_distribution<RealType, Policy>& dist, const
if(false == detail::check_df(
"boost::math::pdf(const students_t_distribution<%1%>&, %1%)", degrees_of_freedom, &error_result, Policy()))
return error_result;
if(false == detail::check_x(
"boost::math::pdf(const students_t_distribution<%1%>&, %1%)", t, &error_result, Policy()))
return error_result;
// Might conceivably permit df = +infinity and use normal distribution.
RealType result;
RealType basem1 = t * t / degrees_of_freedom;
@@ -111,6 +114,9 @@ inline RealType cdf(const students_t_distribution<RealType, Policy>& dist, const
if(false == detail::check_df(
"boost::math::cdf(const students_t_distribution<%1%>&, %1%)", degrees_of_freedom, &error_result, Policy()))
return error_result;
if(false == detail::check_x(
"boost::math::pdf(const students_t_distribution<%1%>&, %1%)", t, &error_result, Policy()))
return error_result;
if (t == 0)
{

View File

@@ -30,6 +30,7 @@
#include <boost/math/distributions/pareto.hpp>
using boost::math::pareto_distribution;
#include <boost/math/tools/test.hpp>
#include "test_out_of_range.hpp"
#include <iostream>
using std::cout;
@@ -233,6 +234,7 @@ void test_spots(RealType)
// Check kurtosis excess = kurtosis - 3;
// Error condition checks:
check_out_of_range<pareto_distribution<RealType> >(1, 1);
BOOST_CHECK_THROW(pdf(pareto_distribution<RealType>(0, 1), 0), std::domain_error);
BOOST_CHECK_THROW(pdf(pareto_distribution<RealType>(1, 0), 0), std::domain_error);
BOOST_CHECK_THROW(pdf(pareto_distribution<RealType>(-1, 1), 0), std::domain_error);

View File

@@ -34,6 +34,7 @@
#include <boost/math/special_functions/gamma.hpp> // for (incomplete) gamma.
// using boost::math::qamma_Q;
#include "table_type.hpp"
#include "test_out_of_range.hpp"
#include <iostream>
using std::cout;
@@ -485,7 +486,7 @@ void test_spots(RealType)
x = quantile(complement(p6, poisson_quantile_data[i][1]));
BOOST_CHECK_EQUAL(x, floor(poisson_quantile_data[i][3] + 0.5f));
}
check_out_of_range<poisson_distribution<RealType> >(1);
} // template <class RealType>void test_spots(RealType)
//

View File

@@ -18,6 +18,7 @@
#include <boost/test/test_exec_monitor.hpp> // Boost.Test
#include <boost/test/floating_point_comparison.hpp>
#include "test_out_of_range.hpp"
#include <iostream>
using std::cout;
@@ -58,6 +59,13 @@ void test_spot(RealType s, RealType x, RealType p, RealType q, RealType toleranc
x,
tolerance); // %
}
if(std::numeric_limits<RealType>::has_infinity)
{
RealType inf = std::numeric_limits<RealType>::infinity();
BOOST_CHECK_EQUAL(pdf(rayleigh_distribution<RealType>(s), inf), 0);
BOOST_CHECK_EQUAL(cdf(rayleigh_distribution<RealType>(s), inf), 1);
BOOST_CHECK_EQUAL(cdf(complement(rayleigh_distribution<RealType>(s), inf)), 0);
}
} // void test_spot
template <class RealType>
@@ -79,6 +87,7 @@ void test_spots(RealType T)
// Things that are errors:
rayleigh_distribution<RealType> dist(0.5);
check_out_of_range<rayleigh_distribution<RealType> >(1);
BOOST_CHECK_THROW(
quantile(dist,
RealType(1.)), // quantile unity should overflow.

View File

@@ -30,6 +30,7 @@ using std::endl;
using std::setprecision;
#include <limits>
using std::numeric_limits;
#include "test_out_of_range.hpp"
template <class RealType>
void check_skew_normal(RealType mean, RealType scale, RealType shape, RealType x, RealType p, RealType q, RealType tol)
@@ -440,6 +441,7 @@ void test_spots(RealType)
BOOST_CHECK_THROW(cdf(skew_normal_distribution<RealType>(0, -1, 0), 0), std::domain_error);
BOOST_CHECK_THROW(quantile(skew_normal_distribution<RealType>(0, 1, 0), -1), std::domain_error);
BOOST_CHECK_THROW(quantile(skew_normal_distribution<RealType>(0, 1, 0), 2), std::domain_error);
check_out_of_range<skew_normal_distribution<RealType> >(1, 1, 1);
}

View File

@@ -21,6 +21,7 @@
#include <boost/math/distributions/students_t.hpp>
using boost::math::students_t_distribution;
#include <boost/math/tools/test.hpp> // for real_concept
#include "test_out_of_range.hpp"
#include <iostream>
using std::cout;
@@ -469,6 +470,7 @@ void test_spots(RealType)
BOOST_CHECK_THROW(quantile(dist, 2), std::domain_error);
BOOST_CHECK_THROW(pdf(students_t_distribution<RealType>(0), 0), std::domain_error);
BOOST_CHECK_THROW(pdf(students_t_distribution<RealType>(-1), 0), std::domain_error);
check_out_of_range<students_t_distribution<RealType> >(1);
} // template <class RealType>void test_spots(RealType)
int test_main(int, char* [])

View File

@@ -23,6 +23,7 @@
using boost::math::triangular_distribution;
#include <boost/math/tools/test.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include "test_out_of_range.hpp"
#include <iostream>
using std::cout;
@@ -524,6 +525,7 @@ void test_spots(RealType)
}
BOOST_CHECK_THROW(triangular_distribution<RealType>(1, 0), std::domain_error); // lower > upper!
check_out_of_range<triangular_distribution<RealType> >(-1, 0, 1);
} // template <class RealType>void test_spots(RealType)
int test_main(int, char* [])

View File

@@ -22,6 +22,7 @@
#include <boost/math/distributions/uniform.hpp>
using boost::math::uniform_distribution;
#include <boost/math/tools/test.hpp>
#include "test_out_of_range.hpp"
#include <iostream>
using std::cout;
@@ -367,6 +368,7 @@ void test_spots(RealType)
BOOST_CHECK_THROW(uniform_distribution<RealType>(1, 0), std::domain_error); // lower > upper!
BOOST_CHECK_THROW(uniform_distribution<RealType>(1, 1), std::domain_error); // lower == upper!
check_out_of_range<uniform_distribution<RealType> >(1, 5);
} // template <class RealType>void test_spots(RealType)
int test_main(int, char* [])

View File

@@ -20,6 +20,7 @@
#include <boost/math/distributions/weibull.hpp>
using boost::math::weibull_distribution;
#include <boost/math/tools/test.hpp>
#include "test_out_of_range.hpp"
#include <iostream>
using std::cout;
@@ -342,41 +343,7 @@ void test_spots(RealType)
BOOST_CHECK_EQUAL(pdf(weibull_distribution<RealType>(1, 3), 0), exp(-pow(RealType(0) / RealType(3), RealType(1))) * pow(RealType(0), RealType(0)) * RealType(1) / RealType(3));
BOOST_CHECK_THROW(pdf(weibull_distribution<RealType>(0.5, 3), 0), std::overflow_error);
// No longer allow any parameter to be NaN or inf, so all these tests should throw.
if (std::numeric_limits<RealType>::has_quiet_NaN)
{
// Attempt to construct from non-finite should throw.
RealType nan = std::numeric_limits<RealType>::quiet_NaN();
BOOST_CHECK_THROW(weibull_distribution<RealType> w(nan), std::domain_error);
BOOST_CHECK_THROW(weibull_distribution<RealType> w(1, nan), std::domain_error);
// Non-finite parameters should throw.
weibull_distribution<RealType> w(RealType(1));
BOOST_CHECK_THROW(pdf(w, +nan), std::domain_error); // x = NaN
BOOST_CHECK_THROW(cdf(w, +nan), std::domain_error); // x = NaN
BOOST_CHECK_THROW(cdf(complement(w, +nan)), std::domain_error); // x = + nan
BOOST_CHECK_THROW(quantile(w, +nan), std::domain_error); // p = + nan
BOOST_CHECK_THROW(quantile(complement(w, +nan)), std::domain_error); // p = + nan
} // has_quiet_NaN
if (std::numeric_limits<RealType>::has_infinity)
{
RealType inf = std::numeric_limits<RealType>::infinity();
BOOST_CHECK_THROW(weibull_distribution<RealType> w(inf), std::domain_error);
BOOST_CHECK_THROW(weibull_distribution<RealType> w(1, inf), std::domain_error);
weibull_distribution<RealType> w(RealType(1));
BOOST_CHECK_THROW(weibull_distribution<RealType> w(inf), std::domain_error);
BOOST_CHECK_THROW(weibull_distribution<RealType> w(1, inf), std::domain_error);
BOOST_CHECK_THROW(pdf(w, +inf), std::domain_error); // x = inf
BOOST_CHECK_THROW(cdf(w, +inf), std::domain_error); // x = inf
BOOST_CHECK_THROW(cdf(complement(w, +inf)), std::domain_error); // x = + inf
BOOST_CHECK_THROW(quantile(w, +inf), std::domain_error); // p = + inf
BOOST_CHECK_THROW(quantile(complement(w, +inf)), std::domain_error); // p = + inf
} // has_infinity
check_out_of_range<weibull_distribution<RealType> >(1, 1);
} // template <class RealType>void test_spots(RealType)
int test_main(int, char* [])