mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-03 12:24:26 +02:00
ratio_pow and ratio_sqrt fixed + unit tests added
This commit is contained in:
@@ -128,10 +128,15 @@ namespace std::experimental::units {
|
||||
};
|
||||
|
||||
template<typename R>
|
||||
struct ratio_pow_impl<R, 0> {
|
||||
struct ratio_pow_impl<R, 1> {
|
||||
using type = R;
|
||||
};
|
||||
|
||||
template<typename R>
|
||||
struct ratio_pow_impl<R, 0> {
|
||||
using type = ratio<1>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R, std::size_t N>
|
||||
@@ -158,10 +163,20 @@ namespace std::experimental::units {
|
||||
return sqrt_impl(v, 1, v);
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
struct ratio_sqrt_impl {
|
||||
using type = ratio<detail::sqrt_impl(R::num), detail::sqrt_impl(R::den)>;
|
||||
};
|
||||
|
||||
template<std::intmax_t Den>
|
||||
struct ratio_sqrt_impl<ratio<0, Den>> {
|
||||
using type = ratio<0>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R>
|
||||
using ratio_sqrt = ratio<detail::sqrt_impl(R::den), detail::sqrt_impl(R::num)>;
|
||||
using ratio_sqrt = detail::ratio_sqrt_impl<R>::type;
|
||||
|
||||
|
||||
// common_ratio
|
||||
|
@@ -25,7 +25,7 @@ add_library(unit_tests
|
||||
test_custom_units.cpp
|
||||
test_dimension.cpp
|
||||
test_quantity.cpp
|
||||
test_tools.cpp
|
||||
test_ratio.cpp
|
||||
test_type_list.cpp
|
||||
test_units.cpp
|
||||
)
|
||||
|
@@ -41,6 +41,21 @@ namespace {
|
||||
static_assert(std::is_same_v<ratio_divide<ratio<1, 8>, ratio<2>>, ratio<1, 16>>);
|
||||
static_assert(std::is_same_v<ratio_divide<ratio<6>, ratio<3>>, ratio<2>>);
|
||||
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<2>, 0>, ratio<1>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<2>, 1>, ratio<2>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<2>, 2>, ratio<4>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<2>, 3>, ratio<8>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<1, 2>, 0>, ratio<1>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<1, 2>, 1>, ratio<1, 2>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<1, 2>, 2>, ratio<1, 4>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<1, 2>, 3>, ratio<1, 8>>);
|
||||
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<9>>, ratio<3>>);
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<4>>, ratio<2>>);
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<1>>, ratio<1>>);
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<0>>, ratio<0>>);
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<1, 4>>, ratio<1, 2>>);
|
||||
|
||||
// common_ratio
|
||||
|
||||
static_assert(std::is_same_v<common_ratio<ratio<1>, ratio<1000>>, ratio<1>>);
|
Reference in New Issue
Block a user