pow.hpp 2.9 KB

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