2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Improve powm1 error handling.

Makes 0^-n an overflow error (which matches std::pow which returns +INF rather than a NaN).
Fixes https://github.com/boostorg/math/issues/781.
This commit is contained in:
jzmaddock
2022-11-24 18:43:32 +00:00
parent 23b1fba8e7
commit aad4f85955
2 changed files with 10 additions and 2 deletions

View File

@@ -281,6 +281,8 @@ when T1 and T2 are different types.
There are two domains where this is useful: when /y/ is very small, or when
/x/ is close to 1.
Note that for invalid input this function may raise a __domain_error or __overflow_error as appropriate.
Implemented in terms of `expm1`.
The following graph illustrates the behaviour of powm1:

View File

@@ -16,6 +16,7 @@
#include <boost/math/special_functions/log1p.hpp>
#include <boost/math/special_functions/expm1.hpp>
#include <boost/math/special_functions/trunc.hpp>
#include <boost/math/special_functions/sign.hpp>
#include <boost/math/tools/assert.hpp>
namespace boost{ namespace math{ namespace detail{
@@ -40,7 +41,7 @@ inline T powm1_imp(const T x, const T y, const Policy& pol)
// fall through....
}
}
else if (x < 0)
else if (boost::math::signbit(x)) // Need to error check -0 here as well
{
// y had better be an integer:
if (boost::math::trunc(y) != y)
@@ -48,7 +49,12 @@ inline T powm1_imp(const T x, const T y, const Policy& pol)
if (boost::math::trunc(y / 2) == y / 2)
return powm1_imp(T(-x), y, pol);
}
return pow(x, y) - 1;
T result = pow(x, y) - 1;
if((boost::math::isinf)(result))
return result < 0 ? -boost::math::policies::raise_overflow_error<T>(function, nullptr, pol) : boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
if((boost::math::isnan)(result))
return boost::math::policies::raise_domain_error<T>(function, "Result of pow is complex or undefined", x, pol);
return result;
}
} // detail