cbrt.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_SF_CBRT_HPP
  6. #define BOOST_MATH_SF_CBRT_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/tools/rational.hpp>
  11. #include <boost/math/policies/error_handling.hpp>
  12. #include <boost/math/special_functions/math_fwd.hpp>
  13. #include <boost/math/special_functions/fpclassify.hpp>
  14. #include <boost/mpl/divides.hpp>
  15. #include <boost/mpl/plus.hpp>
  16. #include <boost/mpl/if.hpp>
  17. #include <boost/type_traits/is_convertible.hpp>
  18. namespace boost{ namespace math{
  19. namespace detail
  20. {
  21. struct big_int_type
  22. {
  23. operator boost::uintmax_t()const;
  24. };
  25. template <class T>
  26. struct largest_cbrt_int_type
  27. {
  28. typedef typename mpl::if_<
  29. boost::is_convertible<big_int_type, T>,
  30. boost::uintmax_t,
  31. unsigned int
  32. >::type type;
  33. };
  34. template <class T, class Policy>
  35. T cbrt_imp(T z, const Policy& pol)
  36. {
  37. BOOST_MATH_STD_USING
  38. //
  39. // cbrt approximation for z in the range [0.5,1]
  40. // It's hard to say what number of terms gives the optimum
  41. // trade off between precision and performance, this seems
  42. // to be about the best for double precision.
  43. //
  44. // Maximum Deviation Found: 1.231e-006
  45. // Expected Error Term: -1.231e-006
  46. // Maximum Relative Change in Control Points: 5.982e-004
  47. //
  48. static const T P[] = {
  49. static_cast<T>(0.37568269008611818),
  50. static_cast<T>(1.3304968705558024),
  51. static_cast<T>(-1.4897101632445036),
  52. static_cast<T>(1.2875573098219835),
  53. static_cast<T>(-0.6398703759826468),
  54. static_cast<T>(0.13584489959258635),
  55. };
  56. static const T correction[] = {
  57. static_cast<T>(0.62996052494743658238360530363911), // 2^-2/3
  58. static_cast<T>(0.79370052598409973737585281963615), // 2^-1/3
  59. static_cast<T>(1),
  60. static_cast<T>(1.2599210498948731647672106072782), // 2^1/3
  61. static_cast<T>(1.5874010519681994747517056392723), // 2^2/3
  62. };
  63. if((boost::math::isinf)(z) || (z == 0))
  64. return z;
  65. if(!(boost::math::isfinite)(z))
  66. {
  67. return policies::raise_domain_error("boost::math::cbrt<%1%>(%1%)", "Argument to function must be finite but got %1%.", z, pol);
  68. }
  69. int i_exp, sign(1);
  70. if(z < 0)
  71. {
  72. z = -z;
  73. sign = -sign;
  74. }
  75. T guess = frexp(z, &i_exp);
  76. int original_i_exp = i_exp; // save for later
  77. guess = tools::evaluate_polynomial(P, guess);
  78. int i_exp3 = i_exp / 3;
  79. typedef typename largest_cbrt_int_type<T>::type shift_type;
  80. BOOST_STATIC_ASSERT( ::std::numeric_limits<shift_type>::radix == 2);
  81. if(abs(i_exp3) < std::numeric_limits<shift_type>::digits)
  82. {
  83. if(i_exp3 > 0)
  84. guess *= shift_type(1u) << i_exp3;
  85. else
  86. guess /= shift_type(1u) << -i_exp3;
  87. }
  88. else
  89. {
  90. guess = ldexp(guess, i_exp3);
  91. }
  92. i_exp %= 3;
  93. guess *= correction[i_exp + 2];
  94. //
  95. // Now inline Halley iteration.
  96. // We do this here rather than calling tools::halley_iterate since we can
  97. // simplify the expressions algebraically, and don't need most of the error
  98. // checking of the boilerplate version as we know in advance that the function
  99. // is well behaved...
  100. //
  101. typedef typename policies::precision<T, Policy>::type prec;
  102. typedef typename mpl::divides<prec, mpl::int_<3> >::type prec3;
  103. typedef typename mpl::plus<prec3, mpl::int_<3> >::type new_prec;
  104. typedef typename policies::normalise<Policy, policies::digits2<new_prec::value> >::type new_policy;
  105. //
  106. // Epsilon calculation uses compile time arithmetic when it's available for type T,
  107. // otherwise uses ldexp to calculate at runtime:
  108. //
  109. T eps = (new_prec::value > 3) ? policies::get_epsilon<T, new_policy>() : ldexp(T(1), -2 - tools::digits<T>() / 3);
  110. T diff;
  111. if(original_i_exp < std::numeric_limits<T>::max_exponent - 3)
  112. {
  113. //
  114. // Safe from overflow, use the fast method:
  115. //
  116. do
  117. {
  118. T g3 = guess * guess * guess;
  119. diff = (g3 + z + z) / (g3 + g3 + z);
  120. guess *= diff;
  121. }
  122. while(fabs(1 - diff) > eps);
  123. }
  124. else
  125. {
  126. //
  127. // Either we're ready to overflow, or we can't tell because numeric_limits isn't
  128. // available for type T:
  129. //
  130. do
  131. {
  132. T g2 = guess * guess;
  133. diff = (g2 - z / guess) / (2 * guess + z / g2);
  134. guess -= diff;
  135. }
  136. while((guess * eps) < fabs(diff));
  137. }
  138. return sign * guess;
  139. }
  140. } // namespace detail
  141. template <class T, class Policy>
  142. inline typename tools::promote_args<T>::type cbrt(T z, const Policy& pol)
  143. {
  144. typedef typename tools::promote_args<T>::type result_type;
  145. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  146. return static_cast<result_type>(detail::cbrt_imp(value_type(z), pol));
  147. }
  148. template <class T>
  149. inline typename tools::promote_args<T>::type cbrt(T z)
  150. {
  151. return cbrt(z, policies::policy<>());
  152. }
  153. } // namespace math
  154. } // namespace boost
  155. #endif // BOOST_MATH_SF_CBRT_HPP