Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:38:47

0001 //  (C) Copyright John Maddock 2006.
0002 //  (C) Copyright Matt Borland 2024.
0003 //  Use, modification and distribution are subject to the
0004 //  Boost Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_MATH_POWM1
0008 #define BOOST_MATH_POWM1
0009 
0010 #ifdef _MSC_VER
0011 #pragma once
0012 #pragma warning(push)
0013 #pragma warning(disable:4702) // Unreachable code (release mode only warning)
0014 #endif
0015 
0016 #include <boost/math/tools/config.hpp>
0017 #include <boost/math/special_functions/math_fwd.hpp>
0018 #include <boost/math/special_functions/log1p.hpp>
0019 #include <boost/math/special_functions/expm1.hpp>
0020 #include <boost/math/special_functions/trunc.hpp>
0021 #include <boost/math/special_functions/sign.hpp>
0022 #include <boost/math/tools/assert.hpp>
0023 
0024 namespace boost{ namespace math{ namespace detail{
0025 
0026 template <class T, class Policy>
0027 BOOST_MATH_GPU_ENABLED inline T powm1_imp(const T x, const T y, const Policy& pol)
0028 {
0029    BOOST_MATH_STD_USING
0030    constexpr auto function = "boost::math::powm1<%1%>(%1%, %1%)";
0031 
0032    if ((fabs(y * (x - 1)) < T(0.5)) || (fabs(y) < T(0.2)))
0033    {
0034       // We don't have any good/quick approximation for log(x) * y
0035       // so just try it and see:
0036       T l = y * log(x);
0037       if (l < T(0.5))
0038          return boost::math::expm1(l, pol);
0039       if (l > boost::math::tools::log_max_value<T>())
0040          return boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0041       // fall through....
0042    }
0043    
0044    T result = pow(x, y) - 1;
0045    if((boost::math::isinf)(result))
0046       return result < 0 ? -boost::math::policies::raise_overflow_error<T>(function, nullptr, pol) : boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
0047    if((boost::math::isnan)(result))
0048       return boost::math::policies::raise_domain_error<T>(function, "Result of pow is complex or undefined", x, pol);
0049    return result;
0050 }
0051 
0052 template <class T, class Policy>
0053 BOOST_MATH_GPU_ENABLED inline T powm1_imp_dispatch(const T x, const T y, const Policy& pol)
0054 {
0055    BOOST_MATH_STD_USING
0056 
0057    if ((boost::math::signbit)(x)) // Need to error check -0 here as well
0058    {
0059       constexpr auto function = "boost::math::powm1<%1%>(%1%, %1%)";
0060 
0061       // y had better be an integer:
0062       if (boost::math::trunc(y) != y)
0063          return boost::math::policies::raise_domain_error<T>(function, "For non-integral exponent, expected base > 0 but got %1%", x, pol);
0064       if (boost::math::trunc(y / 2) == y / 2)
0065          return powm1_imp(T(-x), T(y), pol);
0066    }
0067 
0068    return powm1_imp(T(x), T(y), pol);
0069 }
0070 
0071 } // detail
0072 
0073 template <class T1, class T2>
0074 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
0075    powm1(const T1 a, const T2 z)
0076 {
0077    typedef typename tools::promote_args<T1, T2>::type result_type;
0078    return detail::powm1_imp_dispatch(static_cast<result_type>(a), static_cast<result_type>(z), policies::policy<>());
0079 }
0080 
0081 template <class T1, class T2, class Policy>
0082 BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T1, T2>::type
0083    powm1(const T1 a, const T2 z, const Policy& pol)
0084 {
0085    typedef typename tools::promote_args<T1, T2>::type result_type;
0086    return detail::powm1_imp_dispatch(static_cast<result_type>(a), static_cast<result_type>(z), pol);
0087 }
0088 
0089 } // namespace math
0090 } // namespace boost
0091 
0092 #ifdef _MSC_VER
0093 #pragma warning(pop)
0094 #endif
0095 
0096 #endif // BOOST_MATH_POWM1
0097 
0098 
0099 
0100 
0101