mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-02 20:04:27 +02:00
derived_unit helper added
This commit is contained in:
@@ -100,15 +100,15 @@ namespace units {
|
||||
// common_ratio
|
||||
|
||||
// todo: simplified
|
||||
template<Ratio Ratio1, Ratio Ratio2>
|
||||
template<Ratio R1, Ratio R2>
|
||||
struct common_ratio {
|
||||
using gcd_num = static_gcd<Ratio1::num, Ratio2::num>;
|
||||
using gcd_den = static_gcd<Ratio1::den, Ratio2::den>;
|
||||
using type = std::ratio<gcd_num::value, (Ratio1::den / gcd_den::value) * Ratio2::den>;
|
||||
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 Ratio1, Ratio Ratio2>
|
||||
using common_ratio_t = typename common_ratio<Ratio1, Ratio2>::type;
|
||||
template<Ratio R1, Ratio R2>
|
||||
using common_ratio_t = typename common_ratio<R1, R2>::type;
|
||||
|
||||
// upcasting
|
||||
|
||||
|
@@ -39,7 +39,7 @@ namespace units {
|
||||
struct millihertz : unit<dimension_frequency, std::milli> {};
|
||||
template<> struct upcasting_traits<upcast_from<millihertz>> : upcast_to<millihertz> {};
|
||||
|
||||
struct hertz : unit<dimension_frequency, std::ratio<1>> {};
|
||||
struct hertz : derived_unit<dimension_frequency, second> {};
|
||||
template<> struct upcasting_traits<upcast_from<hertz>> : upcast_to<hertz> {};
|
||||
|
||||
struct kilohertz : unit<dimension_frequency, std::kilo> {};
|
||||
|
@@ -50,4 +50,66 @@ namespace units {
|
||||
std::is_empty_v<T> &&
|
||||
detail::is_unit<upcast_from<T>>;
|
||||
|
||||
|
||||
// derived_unit
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<Dimension D>
|
||||
struct get_unit_base_dim;
|
||||
|
||||
template<Exponent E, Exponent... Rest>
|
||||
struct get_unit_base_dim<dimension<E, Rest...>> {
|
||||
static_assert(sizeof...(Rest) == 0, "Base unit expected");
|
||||
using dimension = typename E::dimension;
|
||||
};
|
||||
|
||||
template<typename BaseDimension, Unit... Us>
|
||||
struct get_ratio {
|
||||
using ratio = std::ratio<1>;
|
||||
};
|
||||
|
||||
template<typename BaseDimension, Unit U, Unit... Rest>
|
||||
struct get_ratio<BaseDimension, U, Rest...> {
|
||||
using unit_base_dim = typename get_unit_base_dim<typename U::dimension::base_type>::dimension;
|
||||
using ratio = std::conditional_t<unit_base_dim::value == BaseDimension::value, typename U::ratio,
|
||||
typename get_ratio<BaseDimension, Rest...>::ratio>;
|
||||
};
|
||||
|
||||
template<Ratio Result, int UnitExpValue, Ratio UnitRatio>
|
||||
struct ratio_op;
|
||||
|
||||
template<Ratio Result, Ratio UnitRatio>
|
||||
struct ratio_op<Result, 0, UnitRatio> {
|
||||
using ratio = Result;
|
||||
};
|
||||
|
||||
template<Ratio Result, int UnitExpValue, Ratio UnitRatio>
|
||||
struct ratio_op {
|
||||
using calc_ratio = std::conditional_t<(UnitExpValue > 0), std::ratio_multiply<Result, UnitRatio>,
|
||||
std::ratio_divide<Result, UnitRatio>>;
|
||||
static constexpr int value = UnitExpValue > 0 ? UnitExpValue - 1 : UnitExpValue + 1;
|
||||
using ratio = typename ratio_op<calc_ratio, value, UnitRatio>::ratio;
|
||||
};
|
||||
|
||||
template<Dimension D, Unit... Us>
|
||||
struct derived_ratio;
|
||||
|
||||
template<Unit... Us>
|
||||
struct derived_ratio<dimension<>, Us...> {
|
||||
using ratio = std::ratio<1>;
|
||||
};
|
||||
|
||||
template<Exponent E, Exponent... Rest, Unit... Us>
|
||||
struct derived_ratio<dimension<E, Rest...>, Us...> {
|
||||
using rest_ratio = typename derived_ratio<dimension<Rest...>, Us...>::ratio;
|
||||
using e_ratio = typename get_ratio<typename E::dimension, Us...>::ratio;
|
||||
using ratio = typename ratio_op<rest_ratio, E::value, e_ratio>::ratio;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Dimension D, Unit... Us>
|
||||
using derived_unit = unit<D, typename detail::derived_ratio<typename D::base_type, Us...>::ratio>;
|
||||
|
||||
} // namespace units
|
||||
|
@@ -22,7 +22,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/base_dimensions.h>
|
||||
#include <units/length.h>
|
||||
#include <units/time.h>
|
||||
|
||||
@@ -37,13 +36,13 @@ namespace units {
|
||||
template<Unit U = struct meter_per_second, Number Rep = double>
|
||||
using velocity = quantity<dimension_velocity, U, Rep>;
|
||||
|
||||
struct meter_per_second : unit<dimension_velocity, std::ratio<1>> {};
|
||||
struct meter_per_second : derived_unit<dimension_velocity, meter, second> {};
|
||||
template<> struct upcasting_traits<upcast_from<meter_per_second>> : upcast_to<meter_per_second> {};
|
||||
|
||||
struct kilometer_per_hour : unit<dimension_velocity, std::ratio_divide<kilometer::ratio, hour::ratio>> {};
|
||||
struct kilometer_per_hour : derived_unit<dimension_velocity, kilometer, hour> {};
|
||||
template<> struct upcasting_traits<upcast_from<kilometer_per_hour>> : upcast_to<kilometer_per_hour> {};
|
||||
|
||||
struct mile_per_hour : unit<dimension_velocity, std::ratio_divide<mile::ratio, hour::ratio>> {};
|
||||
struct mile_per_hour : derived_unit<dimension_velocity, mile, hour> {};
|
||||
template<> struct upcasting_traits<upcast_from<mile_per_hour>> : upcast_to<mile_per_hour> {};
|
||||
|
||||
inline namespace literals {
|
||||
|
@@ -38,12 +38,7 @@ namespace {
|
||||
|
||||
using namespace units;
|
||||
|
||||
// frequency
|
||||
|
||||
static_assert(2 / 1_s == 2_Hz);
|
||||
static_assert(1000 / 1_s == 1_kHz);
|
||||
static_assert(1 / 1_ms == 1_kHz);
|
||||
static_assert(3.2_GHz == 3'200'000'000_Hz);
|
||||
/* ************** BASE DIMENSIONS **************** */
|
||||
|
||||
// time
|
||||
|
||||
@@ -65,6 +60,17 @@ namespace {
|
||||
|
||||
// static_assert(5_in + 8_cm == 207_mm);
|
||||
|
||||
|
||||
|
||||
/* ************** DERIVED DIMENSIONS **************** */
|
||||
|
||||
// frequency
|
||||
|
||||
static_assert(2 / 1_s == 2_Hz);
|
||||
static_assert(1000 / 1_s == 1_kHz);
|
||||
static_assert(1 / 1_ms == 1_kHz);
|
||||
static_assert(3.2_GHz == 3'200'000'000_Hz);
|
||||
|
||||
// velocity
|
||||
|
||||
static_assert(std::is_same_v<decltype(1_km / 1_s), velocity<unit<dimension_velocity, std::ratio<1000, 1>>, long long int>>);
|
||||
|
Reference in New Issue
Block a user