pow.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #ifndef BOOST_UNITS_POW_HPP
  11. #define BOOST_UNITS_POW_HPP
  12. #include <boost/type_traits/is_integral.hpp>
  13. #include <boost/units/operators.hpp>
  14. #include <boost/units/static_rational.hpp>
  15. #include <boost/units/detail/static_rational_power.hpp>
  16. /// \file
  17. /// \brief Raise values to exponents known at compile-time.
  18. namespace boost {
  19. namespace units {
  20. /// raise a value to a @c static_rational power.
  21. template<class Rat,class Y>
  22. BOOST_CONSTEXPR
  23. inline typename power_typeof_helper<Y,Rat>::type
  24. pow(const Y& x)
  25. {
  26. return power_typeof_helper<Y,Rat>::value(x);
  27. }
  28. /// raise a value to an integer power.
  29. template<long N,class Y>
  30. BOOST_CONSTEXPR
  31. inline typename power_typeof_helper<Y,static_rational<N> >::type
  32. pow(const Y& x)
  33. {
  34. return power_typeof_helper<Y,static_rational<N> >::value(x);
  35. }
  36. #ifndef BOOST_UNITS_DOXYGEN
  37. /// raise @c T to a @c static_rational power.
  38. template<class T, long N,long D>
  39. struct power_typeof_helper<T, static_rational<N,D> >
  40. {
  41. typedef typename mpl::if_<boost::is_integral<T>, double, T>::type internal_type;
  42. typedef detail::static_rational_power_impl<static_rational<N, D>, internal_type> impl;
  43. typedef typename impl::type type;
  44. static BOOST_CONSTEXPR type value(const T& x)
  45. {
  46. return impl::call(x);
  47. }
  48. };
  49. /// raise @c float to a @c static_rational power.
  50. template<long N,long D>
  51. struct power_typeof_helper<float, static_rational<N,D> >
  52. {
  53. // N.B. pathscale doesn't accept inheritance for some reason.
  54. typedef power_typeof_helper<double, static_rational<N,D> > base;
  55. typedef typename base::type type;
  56. static BOOST_CONSTEXPR type value(const double& x)
  57. {
  58. return base::value(x);
  59. }
  60. };
  61. #endif
  62. /// take the @c static_rational root of a value.
  63. template<class Rat,class Y>
  64. BOOST_CONSTEXPR
  65. typename root_typeof_helper<Y,Rat>::type
  66. root(const Y& x)
  67. {
  68. return root_typeof_helper<Y,Rat>::value(x);
  69. }
  70. /// take the integer root of a value.
  71. template<long N,class Y>
  72. BOOST_CONSTEXPR
  73. typename root_typeof_helper<Y,static_rational<N> >::type
  74. root(const Y& x)
  75. {
  76. return root_typeof_helper<Y,static_rational<N> >::value(x);
  77. }
  78. #ifndef BOOST_UNITS_DOXYGEN
  79. /// take @c static_rational root of an @c T
  80. template<class T, long N,long D>
  81. struct root_typeof_helper<T,static_rational<N,D> >
  82. {
  83. // N.B. pathscale doesn't accept inheritance for some reason.
  84. typedef power_typeof_helper<T, static_rational<D,N> > base;
  85. typedef typename base::type type;
  86. static BOOST_CONSTEXPR type value(const T& x)
  87. {
  88. return(base::value(x));
  89. }
  90. };
  91. #endif
  92. } // namespace units
  93. } // namespace boost
  94. #endif // BOOST_UNITS_STATIC_RATIONAL_HPP