// Boost pow.hpp header file // Computes a power with exponent known at compile-time // (C) Copyright Bruno Lalande 2008. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // See http://www.boost.org for updates, documentation, and revision history. #ifndef BOOST_MATH_POW_HPP #define BOOST_MATH_POW_HPP #include #include #include #include namespace boost { namespace math { namespace detail { template struct positive_power; template <> struct positive_power<0> { template static typename tools::promote_args::type result(T) { return 1; } }; template <> struct positive_power<2> { template static typename tools::promote_args::type result(T base) { return base*base; } }; template struct positive_power { template static typename tools::promote_args::type result(T base) { return (N%2) ? base*positive_power::result(base) : positive_power<2>::result( positive_power::result(base) ); } }; template struct power_if_positive { template static typename tools::promote_args::type result(T base, const Policy&) { return positive_power::result(base); } }; template struct power_if_positive { template static typename tools::promote_args::type result(T base, const Policy& policy) { if (!base) { return policies::raise_overflow_error( "boost::math::pow(%1%)", "Attempted to compute a negative power of 0", policy ); } return 1.0/positive_power<-N>::result(base); } }; template struct select_power_if_positive { typedef typename mpl::greater_equal< mpl::int_, mpl::int_<0> >::type is_positive; typedef power_if_positive type; }; } // namespace detail template inline typename tools::promote_args::type pow(T base, const Policy& policy) { return detail::select_power_if_positive::type::result(base, policy); } template inline typename tools::promote_args::type pow(T base) { return pow(base, policies::policy<>()); } } // namespace math } // namespace boost #endif