| 1 |
// (C) Copyright John Maddock 2006. |
| 2 |
// (C) Copyright Matt Borland 2024. |
| 3 |
// Use, modification and distribution are subject to the |
| 4 |
// Boost Software License, Version 1.0. (See accompanying file |
| 5 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
#ifndef BOOST_MATH_POWM1 |
| 8 |
#define BOOST_MATH_POWM1 |
| 9 |
|
| 10 |
#ifdef _MSC_VER |
| 11 |
#pragma once |
| 12 |
#pragma warning(push) |
| 13 |
#pragma warning(disable:4702) // Unreachable code (release mode only warning) |
| 14 |
#endif |
| 15 |
|
| 16 |
#include <boost/math/tools/config.hpp> |
| 17 |
#include <boost/math/special_functions/math_fwd.hpp> |
| 18 |
#include <boost/math/special_functions/log1p.hpp> |
| 19 |
#include <boost/math/special_functions/expm1.hpp> |
| 20 |
#include <boost/math/special_functions/trunc.hpp> |
| 21 |
#include <boost/math/special_functions/sign.hpp> |
| 22 |
#include <boost/math/tools/assert.hpp> |
| 23 |
|
| 24 |
namespace boost{ namespace math{ namespace detail{ |
| 25 |
|
| 26 |
template <class T, class Policy> |
| 27 |
BOOST_MATH_GPU_ENABLED inline T powm1_imp(const T x, const T y, const Policy& pol) |
| 28 |
{ |
| 29 |
BOOST_MATH_STD_USING |
| 30 |
constexpr auto function = "boost::math::powm1<%1%>(%1%, %1%)"; |
| 31 |
|
| 32 |
if ((fabs(y * (x - 1)) < T(0.5)) || (fabs(y) < T(0.2))) |
| 33 |
{ |
| 34 |
// We don't have any good/quick approximation for log(x) * y |
| 35 |
// so just try it and see: |
| 36 |
T l = y * log(x); |
| 37 |
if (l < T(0.5)) |
| 38 |
return boost::math::expm1(l, pol); |
| 39 |
if (l > boost::math::tools::log_max_value<T>()) |
| 40 |
return boost::math::policies::raise_overflow_error<T>(function, nullptr, pol); |
| 41 |
// fall through.... |
| 42 |
} |
| 43 |
|
| 44 |
T result = pow(x, y) - 1; |
| 45 |
if((boost::math::isinf)(result)) |
| 46 |
return result < 0 ? -boost::math::policies::raise_overflow_error<T>(function, nullptr, pol) : boost::math::policies::raise_overflow_error<T>(function, nullptr, pol); |
| 47 |
if((boost::math::isnan)(result)) |
| 48 |
return boost::math::policies::raise_domain_error<T>(function, "Result of pow is complex or undefined", x, pol); |
| 49 |
return result; |
| 50 |
} |
| 51 |
|
| 52 |
template <class T, class Policy> |
| 53 |
BOOST_MATH_GPU_ENABLED inline T powm1_imp_dispatch(const T x, const T y, const Policy& pol) |
| 54 |
{ |
| 55 |
BOOST_MATH_STD_USING |
| 56 |
|
| 57 |
if ((boost::math::signbit)(x)) // Need to error check -0 here as well |
| 58 |
{ |
| 59 |
constexpr auto function = "boost::math::powm1<%1%>(%1%, %1%)"; |
| 60 |
|
| 61 |
// y had better be an integer: |
| 62 |
if (boost::math::trunc(y) != y) |
| 63 |
return boost::math::policies::raise_domain_error<T>(function, "For non-integral exponent, expected base > 0 but got %1%", x, pol); |
| 64 |
if (boost::math::trunc(y / 2) == y / 2) |
| 65 |
return powm1_imp(T(-x), T(y), pol); |
| 66 |
} |
| 67 |
|
| 68 |
return powm1_imp(T(x), T(y), pol); |
| 69 |
} |
| 70 |
|
| 71 |
} // detail |
| 72 |
|
| 73 |
template <class T1, class T2> |
| 74 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type |
| 75 |
powm1(const T1 a, const T2 z) |
| 76 |
{ |
| 77 |
typedef typename tools::promote_args<T1, T2>::type result_type; |
| 78 |
return detail::powm1_imp_dispatch(static_cast<result_type>(a), static_cast<result_type>(z), policies::policy<>()); |
| 79 |
} |
| 80 |
|
| 81 |
template <class T1, class T2, class Policy> |
| 82 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type |
| 83 |
powm1(const T1 a, const T2 z, const Policy& pol) |
| 84 |
{ |
| 85 |
typedef typename tools::promote_args<T1, T2>::type result_type; |
| 86 |
return detail::powm1_imp_dispatch(static_cast<result_type>(a), static_cast<result_type>(z), pol); |
| 87 |
} |
| 88 |
|
| 89 |
} // namespace math |
| 90 |
} // namespace boost |
| 91 |
|
| 92 |
#ifdef _MSC_VER |
| 93 |
#pragma warning(pop) |
| 94 |
#endif |
| 95 |
|
| 96 |
#endif // BOOST_MATH_POWM1 |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|