From db408fde2bdc2157c90cf00380ccb7dc4e1b88a6 Mon Sep 17 00:00:00 2001 From: jzmaddock Date: Fri, 30 Nov 2018 19:54:34 +0000 Subject: [PATCH] roots.hpp: clean up code so that there is no division by zero possible. Fixes: https://github.com/boostorg/math/issues/160. --- include/boost/math/tools/roots.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/boost/math/tools/roots.hpp b/include/boost/math/tools/roots.hpp index 81e846613..804f0101c 100644 --- a/include/boost/math/tools/roots.hpp +++ b/include/boost/math/tools/roots.hpp @@ -514,9 +514,10 @@ namespace detail{ template static T step(const T& x, const T& f0, const T& f1, const T& f2) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(T)) { + using std::fabs; T ratio = f0 / f1; T delta; - if(ratio / x < 0.1) + if((x != 0) && (fabs(ratio / x) < 0.1)) { delta = ratio + (f2 / (2 * f1)) * ratio * ratio; // check second derivative doesn't over compensate: