File indexing completed on 2025-01-18 09:40:20
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MATH_POWM1
0007 #define BOOST_MATH_POWM1
0008
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #pragma warning(push)
0012 #pragma warning(disable:4702)
0013 #endif
0014
0015 #include <boost/math/special_functions/math_fwd.hpp>
0016 #include <boost/math/special_functions/log1p.hpp>
0017 #include <boost/math/special_functions/expm1.hpp>
0018 #include <boost/math/special_functions/trunc.hpp>
0019 #include <boost/math/special_functions/sign.hpp>
0020 #include <boost/math/tools/assert.hpp>
0021
0022 namespace boost{ namespace math{ namespace detail{
0023
0024 template <class T, class Policy>
0025 inline T powm1_imp(const T x, const T y, const Policy& pol)
0026 {
0027 BOOST_MATH_STD_USING
0028 static const char* function = "boost::math::powm1<%1%>(%1%, %1%)";
0029 if (x > 0)
0030 {
0031 if ((fabs(y * (x - 1)) < T(0.5)) || (fabs(y) < T(0.2)))
0032 {
0033
0034
0035 T l = y * log(x);
0036 if (l < T(0.5))
0037 return boost::math::expm1(l, pol);
0038 if (l > boost::math::tools::log_max_value<T>())
0039 return boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0040
0041 }
0042 }
0043 else if ((boost::math::signbit)(x))
0044 {
0045
0046 if (boost::math::trunc(y) != y)
0047 return boost::math::policies::raise_domain_error<T>(function, "For non-integral exponent, expected base > 0 but got %1%", x, pol);
0048 if (boost::math::trunc(y / 2) == y / 2)
0049 return powm1_imp(T(-x), y, pol);
0050 }
0051 T result = pow(x, y) - 1;
0052 if((boost::math::isinf)(result))
0053 return result < 0 ? -boost::math::policies::raise_overflow_error<T>(function, nullptr, pol) : boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0054 if((boost::math::isnan)(result))
0055 return boost::math::policies::raise_domain_error<T>(function, "Result of pow is complex or undefined", x, pol);
0056 return result;
0057 }
0058
0059 }
0060
0061 template <class T1, class T2>
0062 inline typename tools::promote_args<T1, T2>::type
0063 powm1(const T1 a, const T2 z)
0064 {
0065 typedef typename tools::promote_args<T1, T2>::type result_type;
0066 return detail::powm1_imp(static_cast<result_type>(a), static_cast<result_type>(z), policies::policy<>());
0067 }
0068
0069 template <class T1, class T2, class Policy>
0070 inline typename tools::promote_args<T1, T2>::type
0071 powm1(const T1 a, const T2 z, const Policy& pol)
0072 {
0073 typedef typename tools::promote_args<T1, T2>::type result_type;
0074 return detail::powm1_imp(static_cast<result_type>(a), static_cast<result_type>(z), pol);
0075 }
0076
0077 }
0078 }
0079
0080 #ifdef _MSC_VER
0081 #pragma warning(pop)
0082 #endif
0083
0084 #endif
0085
0086
0087
0088
0089