Skip to content

Instantly share code, notes, and snippets.

@m-ou-se
Last active December 27, 2015 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-ou-se/7291008 to your computer and use it in GitHub Desktop.
Save m-ou-se/7291008 to your computer and use it in GitHub Desktop.
std::ratio_power
template<typename R, int p>
struct ratio_power {
private:
using S = typename std::ratio_multiply<R, R>::type;
public:
using type = typename std::ratio_multiply<
typename ratio_power<R, p%2>::type,
typename ratio_power<S, p/2>::type
>::type;
constexpr static intmax_t num = type::num;
constexpr static intmax_t den = type::den;
};
template<typename R>
struct ratio_power<R, 0> {
using type = std::ratio<1>;
constexpr static intmax_t num = type::num;
constexpr static intmax_t den = type::den;
};
template<typename R>
struct ratio_power<R, 1> {
using type = R;
constexpr static intmax_t num = type::num;
constexpr static intmax_t den = type::den;
};
template<typename R>
struct ratio_power<R, -1> {
using type = typename std::ratio_divide<std::ratio<1>, R>::type;
constexpr static intmax_t num = type::num;
constexpr static intmax_t den = type::den;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment