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

source code of include/boost/math/special_functions/powm1.hpp