cbrt.hpp 5.7 KB

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