forked from mpusz/mp-units
Add make_base_power() helper
This commit is contained in:
@@ -85,6 +85,11 @@ constexpr auto make_ratio() {
|
||||
return quotient_t<detail::prime_factorization_t<N>, detail::prime_factorization_t<D>>{};
|
||||
}
|
||||
|
||||
template <typename T, std::intmax_t N = 1, std::intmax_t D = 1>
|
||||
constexpr auto make_base_power() {
|
||||
return magnitude<base_power<T, ratio{N, D}>>{};
|
||||
}
|
||||
|
||||
struct pi {
|
||||
static inline constexpr long double value = std::numbers::pi_v<long double>;
|
||||
};
|
||||
|
@@ -234,6 +234,22 @@ TEST_CASE("make_ratio performs prime factorization correctly")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("make_magnitude handles arbitrary bases")
|
||||
{
|
||||
SECTION("Equivalent to std::integral_constant for integer bases")
|
||||
{
|
||||
CHECK(make_base_power<int_base<2>>() == make_ratio<2>());
|
||||
CHECK(make_base_power<int_base<7>>() == make_ratio<7>());
|
||||
}
|
||||
|
||||
SECTION("Handles non-integer bases")
|
||||
{
|
||||
CHECK(make_base_power<pi>() == magnitude<base_power<pi>>{});
|
||||
CHECK(make_base_power<pi, -3>() == magnitude<base_power<pi, ratio{-3}>>{});
|
||||
CHECK(make_base_power<pi, -3, 7>() == magnitude<base_power<pi, ratio{-3, 7}>>{});
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Equality works for magnitudes")
|
||||
{
|
||||
SECTION("Equivalent ratios are equal")
|
||||
@@ -268,6 +284,14 @@ TEST_CASE("Multiplication works for magnitudes")
|
||||
CHECK(make_ratio<4, 5>() * make_ratio<4, 3>() == make_ratio<16, 15>());
|
||||
}
|
||||
|
||||
SECTION("Products handle pi correctly")
|
||||
{
|
||||
CHECK(
|
||||
make_base_power<pi>() * make_ratio<2, 3>() * make_base_power<pi, -1, 2>() ==
|
||||
magnitude<int_base_power<2>, int_base_power<3, -1>, base_power<pi, ratio{1, 2}>>{});
|
||||
|
||||
}
|
||||
|
||||
SECTION("Supports constexpr")
|
||||
{
|
||||
constexpr auto p = make_ratio<4, 5>() * make_ratio<4, 3>();
|
||||
|
Reference in New Issue
Block a user