Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only solve problems if have enough precision #1004

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions include/boost/math/special_functions/detail/ibeta_inverse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,16 @@ namespace boost{ namespace math{ namespace detail{
template <class T>
struct temme_root_finder
{
temme_root_finder(const T t_, const T a_) : t(t_), a(a_) {}
temme_root_finder(const T t_, const T a_) : t(t_), a(a_) {
const T x_extrema = 1 / (1 + a);
BOOST_MATH_ASSERT(0 < x_extrema && x_extrema < 1);
}

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

T y = 1 - x;
if(y == 0)
ryanelandt marked this conversation as resolved.
Show resolved Hide resolved
{
T big = tools::max_value<T>() / 4;
return boost::math::make_tuple(static_cast<T>(-big), static_cast<T>(-big));
}
if(x == 0)
{
T big = tools::max_value<T>() / 4;
return boost::math::make_tuple(static_cast<T>(-big), big);
}
T f = log(x) + a * log(y) + t;
T f1 = (1 / x) - (a / (y));
return boost::math::make_tuple(f, f1);
Expand Down Expand Up @@ -410,6 +403,10 @@ T temme_method_3_ibeta_inverse(T a, T b, T p, T q, const Policy& pol)
T lower = eta < mu ? cross : 0;
T upper = eta < mu ? 1 : cross;
T x = (lower + upper) / 2;

// Early exit for cases with numerical precision issues.
if (cross == 0 || cross == 1) { return cross; }

x = tools::newton_raphson_iterate(
temme_root_finder<T>(u, mu), x, lower, upper, policies::digits<T, Policy>() / 2);
#ifdef BOOST_INSTRUMENT
Expand Down