Chris@16: // (C) Copyright John Maddock 2006. Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_SF_CBRT_HPP Chris@16: #define BOOST_MATH_SF_CBRT_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: struct big_int_type Chris@16: { Chris@16: operator boost::uintmax_t()const; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct largest_cbrt_int_type Chris@16: { Chris@16: typedef typename mpl::if_< Chris@16: boost::is_convertible, Chris@16: boost::uintmax_t, Chris@16: unsigned int Chris@16: >::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: T cbrt_imp(T z, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: // Chris@16: // cbrt approximation for z in the range [0.5,1] Chris@16: // It's hard to say what number of terms gives the optimum Chris@16: // trade off between precision and performance, this seems Chris@16: // to be about the best for double precision. Chris@16: // Chris@16: // Maximum Deviation Found: 1.231e-006 Chris@16: // Expected Error Term: -1.231e-006 Chris@16: // Maximum Relative Change in Control Points: 5.982e-004 Chris@16: // Chris@16: static const T P[] = { Chris@16: static_cast(0.37568269008611818), Chris@16: static_cast(1.3304968705558024), Chris@16: static_cast(-1.4897101632445036), Chris@16: static_cast(1.2875573098219835), Chris@16: static_cast(-0.6398703759826468), Chris@16: static_cast(0.13584489959258635), Chris@16: }; Chris@16: static const T correction[] = { Chris@16: static_cast(0.62996052494743658238360530363911), // 2^-2/3 Chris@16: static_cast(0.79370052598409973737585281963615), // 2^-1/3 Chris@16: static_cast(1), Chris@16: static_cast(1.2599210498948731647672106072782), // 2^1/3 Chris@16: static_cast(1.5874010519681994747517056392723), // 2^2/3 Chris@16: }; Chris@16: Chris@16: if(!(boost::math::isfinite)(z)) Chris@16: { Chris@16: return policies::raise_domain_error("boost::math::cbrt<%1%>(%1%)", "Argument to function must be finite but got %1%.", z, pol); Chris@16: } Chris@16: Chris@16: int i_exp, sign(1); Chris@16: if(z < 0) Chris@16: { Chris@16: z = -z; Chris@16: sign = -sign; Chris@16: } Chris@16: if(z == 0) Chris@16: return 0; Chris@16: Chris@16: T guess = frexp(z, &i_exp); Chris@16: int original_i_exp = i_exp; // save for later Chris@16: guess = tools::evaluate_polynomial(P, guess); Chris@16: int i_exp3 = i_exp / 3; Chris@16: Chris@16: typedef typename largest_cbrt_int_type::type shift_type; Chris@16: Chris@16: BOOST_STATIC_ASSERT( ::std::numeric_limits::radix == 2); Chris@16: Chris@16: if(abs(i_exp3) < std::numeric_limits::digits) Chris@16: { Chris@16: if(i_exp3 > 0) Chris@16: guess *= shift_type(1u) << i_exp3; Chris@16: else Chris@16: guess /= shift_type(1u) << -i_exp3; Chris@16: } Chris@16: else Chris@16: { Chris@16: guess = ldexp(guess, i_exp3); Chris@16: } Chris@16: i_exp %= 3; Chris@16: guess *= correction[i_exp + 2]; Chris@16: // Chris@16: // Now inline Halley iteration. Chris@16: // We do this here rather than calling tools::halley_iterate since we can Chris@16: // simplify the expressions algebraically, and don't need most of the error Chris@16: // checking of the boilerplate version as we know in advance that the function Chris@16: // is well behaved... Chris@16: // Chris@16: typedef typename policies::precision::type prec; Chris@16: typedef typename mpl::divides >::type prec3; Chris@16: typedef typename mpl::plus >::type new_prec; Chris@16: typedef typename policies::normalise >::type new_policy; Chris@16: // Chris@16: // Epsilon calculation uses compile time arithmetic when it's available for type T, Chris@16: // otherwise uses ldexp to calculate at runtime: Chris@16: // Chris@16: T eps = (new_prec::value > 3) ? policies::get_epsilon() : ldexp(T(1), -2 - tools::digits() / 3); Chris@16: T diff; Chris@16: Chris@16: if(original_i_exp < std::numeric_limits::max_exponent - 3) Chris@16: { Chris@16: // Chris@16: // Safe from overflow, use the fast method: Chris@16: // Chris@16: do Chris@16: { Chris@16: T g3 = guess * guess * guess; Chris@16: diff = (g3 + z + z) / (g3 + g3 + z); Chris@16: guess *= diff; Chris@16: } Chris@16: while(fabs(1 - diff) > eps); Chris@16: } Chris@16: else Chris@16: { Chris@16: // Chris@16: // Either we're ready to overflow, or we can't tell because numeric_limits isn't Chris@16: // available for type T: Chris@16: // Chris@16: do Chris@16: { Chris@16: T g2 = guess * guess; Chris@16: diff = (g2 - z / guess) / (2 * guess + z / g2); Chris@16: guess -= diff; Chris@16: } Chris@16: while((guess * eps) < fabs(diff)); Chris@16: } Chris@16: Chris@16: return sign * guess; Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type cbrt(T z, const Policy& pol) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: typedef typename policies::evaluation::type value_type; Chris@16: return static_cast(detail::cbrt_imp(value_type(z), pol)); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type cbrt(T z) Chris@16: { Chris@16: return cbrt(z, policies::policy<>()); Chris@16: } Chris@16: Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_MATH_SF_CBRT_HPP Chris@16: Chris@16: Chris@16: Chris@16: