powm1.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef BOOST_MATH_POWM1
  6. #define BOOST_MATH_POWM1
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/log1p.hpp>
  11. #include <boost/math/special_functions/expm1.hpp>
  12. #include <boost/math/special_functions/math_fwd.hpp>
  13. #include <boost/assert.hpp>
  14. namespace boost{ namespace math{ namespace detail{
  15. template <class T, class Policy>
  16. inline T powm1_imp(const T a, const T z, const Policy& pol)
  17. {
  18. BOOST_MATH_STD_USING
  19. if((fabs(a) < 1) || (fabs(z) < 1))
  20. {
  21. T p = log(a) * z;
  22. if(fabs(p) < 2)
  23. return boost::math::expm1(p, pol);
  24. // otherwise fall though:
  25. }
  26. return pow(a, z) - 1;
  27. }
  28. } // detail
  29. template <class T1, class T2>
  30. inline typename tools::promote_args<T1, T2>::type
  31. powm1(const T1 a, const T2 z)
  32. {
  33. typedef typename tools::promote_args<T1, T2>::type result_type;
  34. return detail::powm1_imp(static_cast<result_type>(a), static_cast<result_type>(z), policies::policy<>());
  35. }
  36. template <class T1, class T2, class Policy>
  37. inline typename tools::promote_args<T1, T2>::type
  38. powm1(const T1 a, const T2 z, const Policy& pol)
  39. {
  40. typedef typename tools::promote_args<T1, T2>::type result_type;
  41. return detail::powm1_imp(static_cast<result_type>(a), static_cast<result_type>(z), pol);
  42. }
  43. } // namespace math
  44. } // namespace boost
  45. #endif // BOOST_MATH_POWM1