Skip to content

Commit 99773d8

Browse files
committed
FIX: avoid overflow on overflow check in halley_step on Apple M1
1 parent f0933b4 commit 99773d8

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

include/boost/math/tools/roots.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,12 @@ namespace detail {
354354
BOOST_MATH_INSTRUMENT_VARIABLE(denom);
355355
BOOST_MATH_INSTRUMENT_VARIABLE(num);
356356

357-
if ((fabs(num) < 1) && (fabs(denom) >= fabs(num) * tools::max_value<T>()))
357+
// denom/num overflows if:
358+
// |denom| >= |num| * max_value
359+
// RHS may overflow on Apple M1, so rearrange:
360+
// |denom| * 1/max_value >= |num|
361+
constexpr T inv_max_value = 1.0 / tools::max_value<T>();
362+
if ((fabs(num) < 1) && (inv_max_value * fabs(denom) >= fabs(num)))
358363
{
359364
// possible overflow, use Newton step:
360365
delta = f0 / f1;

0 commit comments

Comments
 (0)