1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
3 //
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
5 // Copyright (C) 2008 Steven Watanabe
6 //
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)
10 
11 #ifndef BOOST_UNITS_POW_HPP
12 #define BOOST_UNITS_POW_HPP
13 
14 #include <boost/type_traits/is_integral.hpp>
15 
16 #include <boost/units/operators.hpp>
17 #include <boost/units/static_rational.hpp>
18 #include <boost/units/detail/static_rational_power.hpp>
19 
20 /// \file
21 /// \brief Raise values to exponents known at compile-time.
22 
23 namespace boost {
24 
25 namespace units {
26 
27 /// raise a value to a @c static_rational power.
28 template<class Rat,class Y>
29 BOOST_CONSTEXPR
30 inline typename power_typeof_helper<Y,Rat>::type
pow(const Y & x)31 pow(const Y& x)
32 {
33     return power_typeof_helper<Y,Rat>::value(x);
34 }
35 
36 /// raise a value to an integer power.
37 template<long N,class Y>
38 BOOST_CONSTEXPR
39 inline typename power_typeof_helper<Y,static_rational<N> >::type
pow(const Y & x)40 pow(const Y& x)
41 {
42     return power_typeof_helper<Y,static_rational<N> >::value(x);
43 }
44 
45 #ifndef BOOST_UNITS_DOXYGEN
46 
47 /// raise @c T to a @c static_rational power.
48 template<class T, long N,long D>
49 struct power_typeof_helper<T, static_rational<N,D> >
50 {
51     typedef typename mpl::if_<boost::is_integral<T>, double, T>::type internal_type;
52     typedef detail::static_rational_power_impl<static_rational<N, D>, internal_type> impl;
53     typedef typename impl::type type;
54 
valueboost::units::power_typeof_helper55     static BOOST_CONSTEXPR type value(const T& x)
56     {
57         return impl::call(x);
58     }
59 };
60 
61 /// raise @c float to a @c static_rational power.
62 template<long N,long D>
63 struct power_typeof_helper<float, static_rational<N,D> >
64 {
65     // N.B.  pathscale doesn't accept inheritance for some reason.
66     typedef power_typeof_helper<double, static_rational<N,D> > base;
67     typedef typename base::type type;
valueboost::units::power_typeof_helper68     static BOOST_CONSTEXPR type value(const double& x)
69     {
70         return base::value(x);
71     }
72 };
73 
74 #endif
75 
76 /// take the @c static_rational root of a value.
77 template<class Rat,class Y>
78 BOOST_CONSTEXPR
79 typename root_typeof_helper<Y,Rat>::type
root(const Y & x)80 root(const Y& x)
81 {
82     return root_typeof_helper<Y,Rat>::value(x);
83 }
84 
85 /// take the integer root of a value.
86 template<long N,class Y>
87 BOOST_CONSTEXPR
88 typename root_typeof_helper<Y,static_rational<N> >::type
root(const Y & x)89 root(const Y& x)
90 {
91     return root_typeof_helper<Y,static_rational<N> >::value(x);
92 }
93 
94 #ifndef BOOST_UNITS_DOXYGEN
95 
96 /// take @c static_rational root of an @c T
97 template<class T, long N,long D>
98 struct root_typeof_helper<T,static_rational<N,D> >
99 {
100     // N.B.  pathscale doesn't accept inheritance for some reason.
101     typedef power_typeof_helper<T, static_rational<D,N> > base;
102     typedef typename base::type type;
valueboost::units::root_typeof_helper103     static BOOST_CONSTEXPR type value(const T& x)
104     {
105         return(base::value(x));
106     }
107 };
108 
109 #endif
110 
111 } // namespace units
112 
113 } // namespace boost
114 
115 #endif // BOOST_UNITS_STATIC_RATIONAL_HPP
116