From 15baa10fac8cd5cec1146da5ecfee42f390e2ff5 Mon Sep 17 00:00:00 2001 From: John Maddock Date: Fri, 20 Apr 2012 10:51:55 +0000 Subject: [PATCH] Add checks to discrete quantile finder to handle cases where there is no true answer. Fixes #6805. [SVN r78090] --- .../distributions/detail/inv_discrete_quantile.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/boost/math/distributions/detail/inv_discrete_quantile.hpp b/include/boost/math/distributions/detail/inv_discrete_quantile.hpp index 4d28e5293..9397e7c7c 100644 --- a/include/boost/math/distributions/detail/inv_discrete_quantile.hpp +++ b/include/boost/math/distributions/detail/inv_discrete_quantile.hpp @@ -124,6 +124,8 @@ typename Dist::value_type --count; if(fb == 0) return b; + if(a == b) + return b; // can't go any higher! } else { @@ -135,6 +137,8 @@ typename Dist::value_type --count; if(fa == 0) return a; + if(a == b) + return a; // We can't go any lower than this! } } } @@ -208,7 +212,7 @@ typename Dist::value_type // Zero is to the right of x2, so walk upwards // until we find it: // - while((boost::math::sign)(fb) == (boost::math::sign)(fa)) + while(((boost::math::sign)(fb) == (boost::math::sign)(fa)) && (a != b)) { if(count == 0) policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", b, policy_type()); @@ -228,7 +232,7 @@ typename Dist::value_type // Zero is to the left of a, so walk downwards // until we find it: // - while((boost::math::sign)(fb) == (boost::math::sign)(fa)) + while(((boost::math::sign)(fb) == (boost::math::sign)(fa)) && (a != b)) { if(fabs(a) < tools::min_value()) { @@ -255,6 +259,8 @@ typename Dist::value_type return a; if(fb == 0) return b; + if(a == b) + return b; // Ran out of bounds trying to bracket - there is no answer! // // Adjust bounds so that if we're looking for an integer // result, then both ends round the same way: