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
23namespace boost {
24
25namespace units {
26
27/// raise a value to a @c static_rational power.
28template<class Rat,class Y>
29BOOST_CONSTEXPR
30inline typename power_typeof_helper<Y,Rat>::type
31pow(const Y& x)
32{
33 return power_typeof_helper<Y,Rat>::value(x);
34}
35
36/// raise a value to an integer power.
37template<long N,class Y>
38BOOST_CONSTEXPR
39inline typename power_typeof_helper<Y,static_rational<N> >::type
40pow(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.
48template<class T, long N,long D>
49struct 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
55 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.
62template<long N,long D>
63struct 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;
68 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.
77template<class Rat,class Y>
78BOOST_CONSTEXPR
79typename root_typeof_helper<Y,Rat>::type
80root(const Y& x)
81{
82 return root_typeof_helper<Y,Rat>::value(x);
83}
84
85/// take the integer root of a value.
86template<long N,class Y>
87BOOST_CONSTEXPR
88typename root_typeof_helper<Y,static_rational<N> >::type
89root(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
97template<class T, long N,long D>
98struct 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;
103 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

source code of boost/libs/units/include/boost/units/pow.hpp