cbrt.hpp 4.9 KB

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