powm1.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #pragma warning(push)
  10. #pragma warning(disable:4702) // Unreachable code (release mode only warning)
  11. #endif
  12. #include <boost/math/special_functions/math_fwd.hpp>
  13. #include <boost/math/special_functions/log1p.hpp>
  14. #include <boost/math/special_functions/expm1.hpp>
  15. #include <boost/math/special_functions/trunc.hpp>
  16. #include <boost/math/special_functions/sign.hpp>
  17. #include <boost/math/tools/assert.hpp>
  18. namespace boost{ namespace math{ namespace detail{
  19. template <class T, class Policy>
  20. inline T powm1_imp(const T x, const T y, const Policy& pol)
  21. {
  22. BOOST_MATH_STD_USING
  23. static const char* function = "boost::math::powm1<%1%>(%1%, %1%)";
  24. if (x > 0)
  25. {
  26. if ((fabs(y * (x - 1)) < T(0.5)) || (fabs(y) < T(0.2)))
  27. {
  28. // We don't have any good/quick approximation for log(x) * y
  29. // so just try it and see:
  30. T l = y * log(x);
  31. if (l < T(0.5))
  32. return boost::math::expm1(l, pol);
  33. if (l > boost::math::tools::log_max_value<T>())
  34. return boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
  35. // fall through....
  36. }
  37. }
  38. else if ((boost::math::signbit)(x)) // Need to error check -0 here as well
  39. {
  40. // y had better be an integer:
  41. if (boost::math::trunc(y) != y)
  42. return boost::math::policies::raise_domain_error<T>(function, "For non-integral exponent, expected base > 0 but got %1%", x, pol);
  43. if (boost::math::trunc(y / 2) == y / 2)
  44. return powm1_imp(T(-x), y, pol);
  45. }
  46. T result = pow(x, y) - 1;
  47. if((boost::math::isinf)(result))
  48. return result < 0 ? -boost::math::policies::raise_overflow_error<T>(function, nullptr, pol) : boost::math::policies::raise_overflow_error<T>(function, nullptr, pol);
  49. if((boost::math::isnan)(result))
  50. return boost::math::policies::raise_domain_error<T>(function, "Result of pow is complex or undefined", x, pol);
  51. return result;
  52. }
  53. } // detail
  54. template <class T1, class T2>
  55. inline typename tools::promote_args<T1, T2>::type
  56. powm1(const T1 a, const T2 z)
  57. {
  58. typedef typename tools::promote_args<T1, T2>::type result_type;
  59. return detail::powm1_imp(static_cast<result_type>(a), static_cast<result_type>(z), policies::policy<>());
  60. }
  61. template <class T1, class T2, class Policy>
  62. inline typename tools::promote_args<T1, T2>::type
  63. powm1(const T1 a, const T2 z, const Policy& pol)
  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), pol);
  67. }
  68. } // namespace math
  69. } // namespace boost
  70. #ifdef _MSC_VER
  71. #pragma warning(pop)
  72. #endif
  73. #endif // BOOST_MATH_POWM1