diff options
Diffstat (limited to 'boost/math/special_functions/powm1.hpp')
-rw-r--r-- | boost/math/special_functions/powm1.hpp | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/boost/math/special_functions/powm1.hpp b/boost/math/special_functions/powm1.hpp index f3af3d6e59..fe2fce35d8 100644 --- a/boost/math/special_functions/powm1.hpp +++ b/boost/math/special_functions/powm1.hpp @@ -13,23 +13,40 @@ #include <boost/math/special_functions/math_fwd.hpp> #include <boost/math/special_functions/log1p.hpp> #include <boost/math/special_functions/expm1.hpp> +#include <boost/math/special_functions/trunc.hpp> #include <boost/assert.hpp> namespace boost{ namespace math{ namespace detail{ template <class T, class Policy> -inline T powm1_imp(const T a, const T z, const Policy& pol) +inline T powm1_imp(const T x, const T y, const Policy& pol) { BOOST_MATH_STD_USING + static const char* function = "boost::math::powm1<%1%>(%1%, %1%)"; - if((fabs(a) < 1) || (fabs(z) < 1)) + if (x > 0) { - T p = log(a) * z; - if(fabs(p) < 2) - return boost::math::expm1(p, pol); - // otherwise fall though: + if ((fabs(y * (x - 1)) < 0.5) || (fabs(y) < 0.2)) + { + // We don't have any good/quick approximation for log(x) * y + // so just try it and see: + T l = y * log(x); + if (l < 0.5) + return boost::math::expm1(l); + if (l > boost::math::tools::log_max_value<T>()) + return boost::math::policies::raise_overflow_error<T>(function, 0, pol); + // fall through.... + } } - return pow(a, z) - 1; + else + { + // y had better be an integer: + if (boost::math::trunc(y) != y) + return boost::math::policies::raise_domain_error<T>(function, "For non-integral exponent, expected base > 0 but got %1%", x, pol); + if (boost::math::trunc(y / 2) == y / 2) + return powm1_imp(T(-x), y, pol); + } + return pow(x, y) - 1; } } // detail |