1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
5 // Copyright (C) 2007-2008 Steven Watanabe
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_UNITS_DETAIL_STATIC_RATIONAL_POWER_HPP
12 #define BOOST_UNITS_DETAIL_STATIC_RATIONAL_POWER_HPP
14 #include <boost/config/no_tr1/cmath.hpp>
16 #include <boost/units/detail/one.hpp>
17 #include <boost/units/operators.hpp>
23 template<long N,long D>
24 class static_rational;
28 namespace typeof_pow_adl_barrier {
35 #if defined(BOOST_UNITS_HAS_BOOST_TYPEOF)
36 BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, pow(typeof_::make<Y>(), 0.0))
37 typedef typename nested::type type;
38 #elif defined(BOOST_UNITS_HAS_MWERKS_TYPEOF)
39 typedef __typeof__(pow(typeof_::make<Y>(), 0.0)) type;
40 #elif defined(BOOST_UNITS_HAS_GNU_TYPEOF)
41 typedef typeof(pow(typeof_::make<Y>(), 0.0)) type;
49 template<class R, class Y>
50 struct static_rational_power_impl
52 typedef typename typeof_pow_adl_barrier::typeof_pow<Y>::type type;
53 static type call(const Y& y)
56 return(pow(y, static_cast<double>(R::Numerator) / static_cast<double>(R::Denominator)));
61 struct static_rational_power_impl<R, one>
64 static one call(const one&)
72 struct static_rational_power_impl<static_rational<N, 1>, one>
75 static one call(const one&)
82 template<long N, bool = (N % 2 == 0)>
83 struct static_int_power_impl;
86 struct static_int_power_impl<N, true>
88 template<class Y, class R>
91 typedef typename multiply_typeof_helper<Y, Y>::type square_type;
92 typedef typename static_int_power_impl<(N >> 1)>::template apply<square_type, R> next;
93 typedef typename next::type type;
94 static type call(const Y& y, const R& r)
96 const square_type square = y * y;
97 return(next::call(square, r));
103 struct static_int_power_impl<N, false>
105 template<class Y, class R>
108 typedef typename multiply_typeof_helper<Y, Y>::type square_type;
109 typedef typename multiply_typeof_helper<Y, R>::type new_r;
110 typedef typename static_int_power_impl<(N >> 1)>::template apply<square_type, new_r> next;
111 typedef typename next::type type;
112 static type call(const Y& y, const R& r)
114 const Y square = y * y;
115 return(next::call(square, y * r));
121 struct static_int_power_impl<1, false>
123 template<class Y, class R>
126 typedef typename multiply_typeof_helper<Y, R>::type type;
127 static type call(const Y& y, const R& r)
135 struct static_int_power_impl<0, true>
137 template<class Y, class R>
141 static R call(const Y&, const R& r)
148 template<int N, bool = (N < 0)>
149 struct static_int_power_sign_impl;
152 struct static_int_power_sign_impl<N, false>
157 typedef typename static_int_power_impl<N>::template apply<Y, one> impl;
158 typedef typename impl::type type;
159 static type call(const Y& y)
162 return(impl::call(y, result));
168 struct static_int_power_sign_impl<N, true>
173 typedef typename static_int_power_impl<-N>::template apply<Y, one> impl;
174 typedef typename divide_typeof_helper<one, typename impl::type>::type type;
175 static type call(const Y& y)
178 return(result/impl::call(y, result));
183 template<long N, class Y>
184 struct static_rational_power_impl<static_rational<N, 1>, Y>
186 typedef typename static_int_power_sign_impl<N>::template apply<Y> impl;
187 typedef typename impl::type type;
188 static type call(const Y& y)
190 return(impl::call(y));
194 template<class R, class Y>
195 typename detail::static_rational_power_impl<R, Y>::type static_rational_power(const Y& y)
197 return(detail::static_rational_power_impl<R, Y>::call(y));
200 } // namespace detail