2
0
mirror of https://github.com/boostorg/math.git synced 2026-02-25 04:22:15 +00:00

Merge pull request #875 from boostorg/issue873

Correct root finders in extreme cases.
This commit is contained in:
jzmaddock
2022-11-15 09:00:39 +00:00
committed by GitHub
2 changed files with 40 additions and 6 deletions

View File

@@ -372,13 +372,19 @@ namespace detail {
T bracket_root_towards_max(F f, T guess, const T& f0, T& min, T& max, std::uintmax_t& count) noexcept(BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
{
using std::fabs;
using std::ldexp;
using std::abs;
using std::frexp;
if(count < 2)
return guess - (max + min) / 2; // Not enough counts left to do anything!!
//
// Move guess towards max until we bracket the root, updating min and max as we go:
//
int e;
frexp(max / guess, &e);
e = abs(e);
T guess0 = guess;
T multiplier = 2;
T multiplier = e < 64 ? static_cast<T>(2) : static_cast<T>(ldexp(T(1), e / 32));
T f_current = f0;
if (fabs(min) < fabs(max))
{
@@ -392,7 +398,7 @@ namespace detail {
f_current = -f_current; // There must be a change of sign!
break;
}
multiplier *= 2;
multiplier *= e > 1024 ? 8 : 2;
unpack_0(f(guess), f_current);
}
}
@@ -411,7 +417,7 @@ namespace detail {
f_current = -f_current; // There must be a change of sign!
break;
}
multiplier *= 2;
multiplier *= e > 1024 ? 8 : 2;
unpack_0(f(guess), f_current);
}
}
@@ -429,13 +435,19 @@ namespace detail {
T bracket_root_towards_min(F f, T guess, const T& f0, T& min, T& max, std::uintmax_t& count) noexcept(BOOST_MATH_IS_FLOAT(T) && noexcept(std::declval<F>()(std::declval<T>())))
{
using std::fabs;
using std::ldexp;
using std::abs;
using std::frexp;
if (count < 2)
return guess - (max + min) / 2; // Not enough counts left to do anything!!
//
// Move guess towards min until we bracket the root, updating min and max as we go:
//
int e;
frexp(guess / min, &e);
e = abs(e);
T guess0 = guess;
T multiplier = 2;
T multiplier = e < 64 ? static_cast<T>(2) : static_cast<T>(ldexp(T(1), e / 32));
T f_current = f0;
if (fabs(min) < fabs(max))
@@ -450,7 +462,7 @@ namespace detail {
f_current = -f_current; // There must be a change of sign!
break;
}
multiplier *= 2;
multiplier *= e > 1024 ? 8 : 2;
unpack_0(f(guess), f_current);
}
}
@@ -469,7 +481,7 @@ namespace detail {
f_current = -f_current; // There must be a change of sign!
break;
}
multiplier *= 2;
multiplier *= e > 1024 ? 8 : 2;
unpack_0(f(guess), f_current);
}
}
@@ -615,6 +627,8 @@ namespace detail {
}
delta = bracket_root_towards_min(f, guess, f0, min, max, count);
result = guess - delta;
if (result <= min)
result = float_next(min);
guess = min;
continue;
}
@@ -641,6 +655,8 @@ namespace detail {
}
delta = bracket_root_towards_max(f, guess, f0, min, max, count);
result = guess - delta;
if (result >= max)
result = float_prior(max);
guess = min;
continue;
}