Skip to content

Commit 50ef83a

Browse files
authored
only solve problems if have enough precision (#1004)
1 parent c62682b commit 50ef83a

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

include/boost/math/special_functions/detail/ibeta_inverse.hpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,16 @@ namespace boost{ namespace math{ namespace detail{
2525
template <class T>
2626
struct temme_root_finder
2727
{
28-
temme_root_finder(const T t_, const T a_) : t(t_), a(a_) {}
28+
temme_root_finder(const T t_, const T a_) : t(t_), a(a_) {
29+
const T x_extrema = 1 / (1 + a);
30+
BOOST_MATH_ASSERT(0 < x_extrema && x_extrema < 1);
31+
}
2932

3033
boost::math::tuple<T, T> operator()(T x)
3134
{
3235
BOOST_MATH_STD_USING // ADL of std names
3336

3437
T y = 1 - x;
35-
if(y == 0)
36-
{
37-
T big = tools::max_value<T>() / 4;
38-
return boost::math::make_tuple(static_cast<T>(-big), static_cast<T>(-big));
39-
}
40-
if(x == 0)
41-
{
42-
T big = tools::max_value<T>() / 4;
43-
return boost::math::make_tuple(static_cast<T>(-big), big);
44-
}
4538
T f = log(x) + a * log(y) + t;
4639
T f1 = (1 / x) - (a / (y));
4740
return boost::math::make_tuple(f, f1);
@@ -410,6 +403,10 @@ T temme_method_3_ibeta_inverse(T a, T b, T p, T q, const Policy& pol)
410403
T lower = eta < mu ? cross : 0;
411404
T upper = eta < mu ? 1 : cross;
412405
T x = (lower + upper) / 2;
406+
407+
// Early exit for cases with numerical precision issues.
408+
if (cross == 0 || cross == 1) { return cross; }
409+
413410
x = tools::newton_raphson_iterate(
414411
temme_root_finder<T>(u, mu), x, lower, upper, policies::digits<T, Policy>() / 2);
415412
#ifdef BOOST_INSTRUMENT

0 commit comments

Comments
 (0)