rational powers

This commit is contained in:
dkavolis authored and Mateusz Pusz committed 2020-12-29 17:04:38 +01:00
1 parent d534d2c62e
commit 23dd21465d
9 files changed
+555 -108

No files matched your search

+16
View File
@@ -58,4 +58,20 @@ constexpr Rep fpow10(std::intmax_t exp)
return result;
}
template<std::intmax_t N, typename T>
constexpr T pow_impl(const T& v) noexcept
{
if constexpr (N == 0) {
return T(1);
} else if constexpr (N == 1) {
return v;
} else if constexpr (N < 0) {
return 1 / pow_impl<-N>(v);
} else if constexpr (N % 2 == 0) { // even
return pow_impl<N / 2>(v * v);
} else { // odd
return v * pow_impl<(N - 1) / 2>(v * v);
}
}
} // namespace units::detail