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) 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_POW_HPP
12 #define BOOST_UNITS_POW_HPP
14 #include <boost/type_traits/is_integral.hpp>
16 #include <boost/units/operators.hpp>
17 #include <boost/units/static_rational.hpp>
18 #include <boost/units/detail/static_rational_power.hpp>
21 /// \brief Raise values to exponents known at compile-time.
27 /// raise a value to a @c static_rational power.
28 template<class Rat,class Y>
30 inline typename power_typeof_helper<Y,Rat>::type
33 return power_typeof_helper<Y,Rat>::value(x);
36 /// raise a value to an integer power.
37 template<long N,class Y>
39 inline typename power_typeof_helper<Y,static_rational<N> >::type
42 return power_typeof_helper<Y,static_rational<N> >::value(x);
45 #ifndef BOOST_UNITS_DOXYGEN
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> >
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;
55 static BOOST_CONSTEXPR type value(const T& x)
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> >
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)
70 return base::value(x);
76 /// take the @c static_rational root of a value.
77 template<class Rat,class Y>
79 typename root_typeof_helper<Y,Rat>::type
82 return root_typeof_helper<Y,Rat>::value(x);
85 /// take the integer root of a value.
86 template<long N,class Y>
88 typename root_typeof_helper<Y,static_rational<N> >::type
91 return root_typeof_helper<Y,static_rational<N> >::value(x);
94 #ifndef BOOST_UNITS_DOXYGEN
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> >
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)
105 return(base::value(x));
115 #endif // BOOST_UNITS_STATIC_RATIONAL_HPP