Chris@16
|
1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
|
Chris@16
|
2 // unit/quantity manipulation and conversion
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
|
Chris@16
|
5 // Copyright (C) 2007-2008 Steven Watanabe
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
8 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
9 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_UNITS_DETAIL_STATIC_RATIONAL_POWER_HPP
|
Chris@16
|
12 #define BOOST_UNITS_DETAIL_STATIC_RATIONAL_POWER_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/config/no_tr1/cmath.hpp>
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/units/detail/one.hpp>
|
Chris@16
|
17 #include <boost/units/operators.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 namespace boost {
|
Chris@16
|
20
|
Chris@16
|
21 namespace units {
|
Chris@16
|
22
|
Chris@16
|
23 template<long N,long D>
|
Chris@16
|
24 class static_rational;
|
Chris@16
|
25
|
Chris@16
|
26 namespace detail {
|
Chris@16
|
27
|
Chris@16
|
28 namespace typeof_pow_adl_barrier {
|
Chris@16
|
29
|
Chris@16
|
30 using std::pow;
|
Chris@16
|
31
|
Chris@16
|
32 template<class Y>
|
Chris@16
|
33 struct typeof_pow
|
Chris@16
|
34 {
|
Chris@16
|
35 #if defined(BOOST_UNITS_HAS_BOOST_TYPEOF)
|
Chris@16
|
36 BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, pow(typeof_::make<Y>(), 0.0))
|
Chris@16
|
37 typedef typename nested::type type;
|
Chris@16
|
38 #elif defined(BOOST_UNITS_HAS_MWERKS_TYPEOF)
|
Chris@16
|
39 typedef __typeof__(pow(typeof_::make<Y>(), 0.0)) type;
|
Chris@16
|
40 #elif defined(BOOST_UNITS_HAS_GNU_TYPEOF)
|
Chris@16
|
41 typedef typeof(pow(typeof_::make<Y>(), 0.0)) type;
|
Chris@16
|
42 #else
|
Chris@16
|
43 typedef Y type;
|
Chris@16
|
44 #endif
|
Chris@16
|
45 };
|
Chris@16
|
46
|
Chris@16
|
47 }
|
Chris@16
|
48
|
Chris@16
|
49 template<class R, class Y>
|
Chris@16
|
50 struct static_rational_power_impl
|
Chris@16
|
51 {
|
Chris@16
|
52 typedef typename typeof_pow_adl_barrier::typeof_pow<Y>::type type;
|
Chris@16
|
53 static type call(const Y& y)
|
Chris@16
|
54 {
|
Chris@16
|
55 using std::pow;
|
Chris@16
|
56 return(pow(y, static_cast<double>(R::Numerator) / static_cast<double>(R::Denominator)));
|
Chris@16
|
57 }
|
Chris@16
|
58 };
|
Chris@16
|
59
|
Chris@16
|
60 template<class R>
|
Chris@16
|
61 struct static_rational_power_impl<R, one>
|
Chris@16
|
62 {
|
Chris@16
|
63 typedef one type;
|
Chris@16
|
64 static one call(const one&)
|
Chris@16
|
65 {
|
Chris@16
|
66 one result;
|
Chris@16
|
67 return(result);
|
Chris@16
|
68 }
|
Chris@16
|
69 };
|
Chris@16
|
70
|
Chris@16
|
71 template<long N>
|
Chris@16
|
72 struct static_rational_power_impl<static_rational<N, 1>, one>
|
Chris@16
|
73 {
|
Chris@16
|
74 typedef one type;
|
Chris@16
|
75 static one call(const one&)
|
Chris@16
|
76 {
|
Chris@16
|
77 one result;
|
Chris@16
|
78 return(result);
|
Chris@16
|
79 }
|
Chris@16
|
80 };
|
Chris@16
|
81
|
Chris@16
|
82 template<long N, bool = (N % 2 == 0)>
|
Chris@16
|
83 struct static_int_power_impl;
|
Chris@16
|
84
|
Chris@16
|
85 template<long N>
|
Chris@16
|
86 struct static_int_power_impl<N, true>
|
Chris@16
|
87 {
|
Chris@16
|
88 template<class Y, class R>
|
Chris@16
|
89 struct apply
|
Chris@16
|
90 {
|
Chris@16
|
91 typedef typename multiply_typeof_helper<Y, Y>::type square_type;
|
Chris@16
|
92 typedef typename static_int_power_impl<(N >> 1)>::template apply<square_type, R> next;
|
Chris@16
|
93 typedef typename next::type type;
|
Chris@16
|
94 static type call(const Y& y, const R& r)
|
Chris@16
|
95 {
|
Chris@16
|
96 const square_type square = y * y;
|
Chris@16
|
97 return(next::call(square, r));
|
Chris@16
|
98 }
|
Chris@16
|
99 };
|
Chris@16
|
100 };
|
Chris@16
|
101
|
Chris@16
|
102 template<long N>
|
Chris@16
|
103 struct static_int_power_impl<N, false>
|
Chris@16
|
104 {
|
Chris@16
|
105 template<class Y, class R>
|
Chris@16
|
106 struct apply
|
Chris@16
|
107 {
|
Chris@16
|
108 typedef typename multiply_typeof_helper<Y, Y>::type square_type;
|
Chris@16
|
109 typedef typename multiply_typeof_helper<Y, R>::type new_r;
|
Chris@16
|
110 typedef typename static_int_power_impl<(N >> 1)>::template apply<square_type, new_r> next;
|
Chris@16
|
111 typedef typename next::type type;
|
Chris@16
|
112 static type call(const Y& y, const R& r)
|
Chris@16
|
113 {
|
Chris@16
|
114 const Y square = y * y;
|
Chris@16
|
115 return(next::call(square, y * r));
|
Chris@16
|
116 }
|
Chris@16
|
117 };
|
Chris@16
|
118 };
|
Chris@16
|
119
|
Chris@16
|
120 template<>
|
Chris@16
|
121 struct static_int_power_impl<1, false>
|
Chris@16
|
122 {
|
Chris@16
|
123 template<class Y, class R>
|
Chris@16
|
124 struct apply
|
Chris@16
|
125 {
|
Chris@16
|
126 typedef typename multiply_typeof_helper<Y, R>::type type;
|
Chris@16
|
127 static type call(const Y& y, const R& r)
|
Chris@16
|
128 {
|
Chris@16
|
129 return(y * r);
|
Chris@16
|
130 }
|
Chris@16
|
131 };
|
Chris@16
|
132 };
|
Chris@16
|
133
|
Chris@16
|
134 template<>
|
Chris@16
|
135 struct static_int_power_impl<0, true>
|
Chris@16
|
136 {
|
Chris@16
|
137 template<class Y, class R>
|
Chris@16
|
138 struct apply
|
Chris@16
|
139 {
|
Chris@16
|
140 typedef R type;
|
Chris@16
|
141 static R call(const Y&, const R& r)
|
Chris@16
|
142 {
|
Chris@16
|
143 return(r);
|
Chris@16
|
144 }
|
Chris@16
|
145 };
|
Chris@16
|
146 };
|
Chris@16
|
147
|
Chris@16
|
148 template<int N, bool = (N < 0)>
|
Chris@16
|
149 struct static_int_power_sign_impl;
|
Chris@16
|
150
|
Chris@16
|
151 template<int N>
|
Chris@16
|
152 struct static_int_power_sign_impl<N, false>
|
Chris@16
|
153 {
|
Chris@16
|
154 template<class Y>
|
Chris@16
|
155 struct apply
|
Chris@16
|
156 {
|
Chris@16
|
157 typedef typename static_int_power_impl<N>::template apply<Y, one> impl;
|
Chris@16
|
158 typedef typename impl::type type;
|
Chris@16
|
159 static type call(const Y& y)
|
Chris@16
|
160 {
|
Chris@16
|
161 one result;
|
Chris@16
|
162 return(impl::call(y, result));
|
Chris@16
|
163 }
|
Chris@16
|
164 };
|
Chris@16
|
165 };
|
Chris@16
|
166
|
Chris@16
|
167 template<int N>
|
Chris@16
|
168 struct static_int_power_sign_impl<N, true>
|
Chris@16
|
169 {
|
Chris@16
|
170 template<class Y>
|
Chris@16
|
171 struct apply
|
Chris@16
|
172 {
|
Chris@16
|
173 typedef typename static_int_power_impl<-N>::template apply<Y, one> impl;
|
Chris@16
|
174 typedef typename divide_typeof_helper<one, typename impl::type>::type type;
|
Chris@16
|
175 static type call(const Y& y)
|
Chris@16
|
176 {
|
Chris@16
|
177 one result;
|
Chris@16
|
178 return(result/impl::call(y, result));
|
Chris@16
|
179 }
|
Chris@16
|
180 };
|
Chris@16
|
181 };
|
Chris@16
|
182
|
Chris@16
|
183 template<long N, class Y>
|
Chris@16
|
184 struct static_rational_power_impl<static_rational<N, 1>, Y>
|
Chris@16
|
185 {
|
Chris@16
|
186 typedef typename static_int_power_sign_impl<N>::template apply<Y> impl;
|
Chris@16
|
187 typedef typename impl::type type;
|
Chris@16
|
188 static type call(const Y& y)
|
Chris@16
|
189 {
|
Chris@16
|
190 return(impl::call(y));
|
Chris@16
|
191 }
|
Chris@16
|
192 };
|
Chris@16
|
193
|
Chris@16
|
194 template<class R, class Y>
|
Chris@16
|
195 typename detail::static_rational_power_impl<R, Y>::type static_rational_power(const Y& y)
|
Chris@16
|
196 {
|
Chris@16
|
197 return(detail::static_rational_power_impl<R, Y>::call(y));
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 } // namespace detail
|
Chris@16
|
201
|
Chris@16
|
202 } // namespace units
|
Chris@16
|
203
|
Chris@16
|
204 } // namespace boost
|
Chris@16
|
205
|
Chris@16
|
206 #endif
|