mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-31 19:04:27 +02:00
common_ratio refactored
This commit is contained in:
@@ -69,16 +69,19 @@ namespace units {
|
||||
concept bool Ratio = detail::is_ratio<T>;
|
||||
|
||||
// common_ratio
|
||||
namespace detail {
|
||||
|
||||
// todo: simplified
|
||||
template<Ratio R1, Ratio R2>
|
||||
struct common_ratio {
|
||||
using gcd_num = static_gcd<R1::num, R2::num>;
|
||||
using gcd_den = static_gcd<R1::den, R2::den>;
|
||||
using type = std::ratio<gcd_num::value, (R1::den / gcd_den::value) * R2::den>;
|
||||
};
|
||||
// todo: simplified
|
||||
template<typename R1, typename R2>
|
||||
struct common_ratio_impl {
|
||||
using gcd_num = static_gcd<R1::num, R2::num>;
|
||||
using gcd_den = static_gcd<R1::den, R2::den>;
|
||||
using type = std::ratio<gcd_num::value, (R1::den / gcd_den::value) * R2::den>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using common_ratio_t = common_ratio<R1, R2>::type;
|
||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
||||
|
||||
} // namespace units
|
||||
|
@@ -67,7 +67,7 @@ namespace units {
|
||||
|
||||
template<Dimension D, Unit U1, Scalar Rep1, Unit U2, Scalar Rep2, Scalar Rep>
|
||||
struct common_quantity<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, Rep> {
|
||||
using type = quantity<D, upcasting_traits_t<unit<D, common_ratio_t<typename U1::ratio, typename U2::ratio>>>, Rep>;
|
||||
using type = quantity<D, upcasting_traits_t<unit<D, common_ratio<typename U1::ratio, typename U2::ratio>>>, Rep>;
|
||||
};
|
||||
|
||||
template<Quantity Q1, Quantity Q2, Scalar Rep = std::common_type_t<typename Q1::rep, typename Q2::rep>>
|
||||
|
@@ -40,9 +40,9 @@ namespace {
|
||||
|
||||
// common_ratio
|
||||
|
||||
static_assert(std::is_same_v<common_ratio_t<std::ratio<1>, std::kilo>, std::ratio<1>>);
|
||||
static_assert(std::is_same_v<common_ratio_t<std::kilo, std::ratio<1>>, std::ratio<1>>);
|
||||
static_assert(std::is_same_v<common_ratio_t<std::ratio<1>, std::milli>, std::milli>);
|
||||
static_assert(std::is_same_v<common_ratio_t<std::milli, std::ratio<1>>, std::milli>);
|
||||
static_assert(std::is_same_v<common_ratio<std::ratio<1>, std::kilo>, std::ratio<1>>);
|
||||
static_assert(std::is_same_v<common_ratio<std::kilo, std::ratio<1>>, std::ratio<1>>);
|
||||
static_assert(std::is_same_v<common_ratio<std::ratio<1>, std::milli>, std::milli>);
|
||||
static_assert(std::is_same_v<common_ratio<std::milli, std::ratio<1>>, std::milli>);
|
||||
|
||||
} // namespace
|
Reference in New Issue
Block a user