Fractional dimension exponents support added

This commit is contained in:
Mateusz Pusz
2019-09-10 21:20:32 +02:00
parent 8587d5350d
commit bce17a8476
5 changed files with 95 additions and 19 deletions

View File

@@ -24,6 +24,8 @@
#include <units/bits/type_list.h>
#include <units/bits/downcasting.h>
#include <units/ratio.h>
#include <ratio>
namespace std::experimental::units {
@@ -60,10 +62,11 @@ namespace std::experimental::units {
// exp
template<const base_dimension& BaseDimension, int Value>
template<const base_dimension& BaseDimension, int Num, int Den = 1>
struct exp {
static constexpr const base_dimension& dimension = BaseDimension;
static constexpr int value = Value;
static constexpr int num = Num;
static constexpr int den = Den;
};
// is_exp
@@ -71,8 +74,8 @@ namespace std::experimental::units {
template<typename T>
inline constexpr bool is_exp = false;
template<const base_dimension& BaseDimension, int Value>
inline constexpr bool is_exp<exp<BaseDimension, Value>> = true;
template<const base_dimension& BaseDimension, int Num, int Den>
inline constexpr bool is_exp<exp<BaseDimension, Num, Den>> = true;
} // namespace detail
template<typename T>
@@ -89,9 +92,9 @@ namespace std::experimental::units {
template<Exponent E>
struct exp_invert;
template<const base_dimension& BaseDimension, int Value>
struct exp_invert<exp<BaseDimension, Value>> {
using type = exp<BaseDimension, -Value>;
template<const base_dimension& BaseDimension, int Num, int Den>
struct exp_invert<exp<BaseDimension, Num, Den>> {
using type = exp<BaseDimension, -Num, Den>;
};
template<Exponent E>
@@ -131,6 +134,7 @@ namespace std::experimental::units {
using dim_invert_t = dim_invert<typename D::base_type>::type;
// todo: force as the only user interface to create dimensions through modules
// make_dimension
namespace detail {
@@ -157,10 +161,14 @@ namespace std::experimental::units {
using type = conditional<std::is_same_v<rest, dimension<>>, dimension<E1>, type_list_push_front<rest, E1>>;
};
template<const base_dimension& D, int V1, int V2, typename... ERest>
struct dim_consolidate<dimension<exp<D, V1>, exp<D, V2>, ERest...>> {
using type = conditional<V1 + V2 == 0, dim_consolidate_t<dimension<ERest...>>,
dim_consolidate_t<dimension<exp<D, V1 + V2>, ERest...>>>;
template<const base_dimension& D, int Num1, int Den1, int Num2, int Den2, typename... ERest>
struct dim_consolidate<dimension<exp<D, Num1, Den1>, exp<D, Num2, Den2>, ERest...>> {
// todo: provide custom implementation for ratio_add
using r1 = std::ratio<Num1, Den1>;
using r2 = std::ratio<Num2, Den2>;
using r = std::ratio_add<r1, r2>;
using type = conditional<r::num == 0, dim_consolidate_t<dimension<ERest...>>,
dim_consolidate_t<dimension<exp<D, r::num, r::den>, ERest...>>>;
};
} // namespace detail

View File

@@ -78,20 +78,20 @@ namespace std::experimental::units {
typename get_ratio<BaseDimension, Rest...>::ratio>;
};
template<typename Result, int UnitExpValue, typename UnitRatio>
template<typename Result, int UnitExpNum, int UnitExpDen, typename UnitRatio>
struct ratio_op;
template<typename Result, typename UnitRatio>
struct ratio_op<Result, 0, UnitRatio> {
template<typename Result, int UnitExpDen, typename UnitRatio>
struct ratio_op<Result, 0, UnitExpDen, UnitRatio> {
using ratio = Result;
};
template<typename Result, int UnitExpValue, typename UnitRatio>
template<typename Result, int UnitExpNum, int UnitExpDen, typename UnitRatio>
struct ratio_op {
using calc_ratio = conditional<(UnitExpValue > 0), ratio_multiply<Result, UnitRatio>,
using calc_ratio = conditional<(UnitExpNum * UnitExpDen > 0), ratio_multiply<Result, UnitRatio>,
ratio_divide<Result, UnitRatio>>;
static constexpr int value = UnitExpValue > 0 ? UnitExpValue - 1 : UnitExpValue + 1;
using ratio = ratio_op<calc_ratio, value, UnitRatio>::ratio;
static constexpr int value = (UnitExpNum * UnitExpDen > 0) ? (UnitExpNum - UnitExpDen) : (UnitExpNum + UnitExpDen);
using ratio = ratio_op<calc_ratio, value, UnitExpDen, UnitRatio>::ratio;
};
template<typename D, typename... Us>
@@ -106,7 +106,7 @@ namespace std::experimental::units {
struct derived_ratio<dimension<E, Rest...>, Us...> {
using rest_ratio = derived_ratio<dimension<Rest...>, Us...>::ratio;
using e_ratio = get_ratio<E::dimension, Us...>::ratio;
using ratio = ratio_op<rest_ratio, E::value, e_ratio>::ratio;
using ratio = ratio_op<rest_ratio, E::num, E::den, e_ratio>::ratio;
};
}

View File

@@ -22,6 +22,7 @@
# unit tests
add_library(unit_tests
test_custom_units.cpp
test_dimension.cpp
test_quantity.cpp
test_tools.cpp

View File

@@ -0,0 +1,64 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <units/dimensions/voltage.h>
/* ************** DERIVED DIMENSIONS THAT INCLUDE UNITS WITH SPECIAL NAMES **************** */
namespace {
using namespace std::experimental::units;
// power spectral density
// todo: add support for make_dimension_t of non base units
// struct power_spectral_density : make_dimension_t<exp<voltage, 2>, exp<frequency, -1>> {};
struct power_spectral_density
: make_dimension_t<exp<base_dim_mass, 2>, exp<base_dim_length, 4>, exp<base_dim_time, -6>, exp<base_dim_time, -1>> {
};
struct sq_volt_per_hertz : unit<power_spectral_density> {};
// amplitude spectral density
// struct dimension_amplitude_spectral_density : make_dimension_t<exp<voltage, 1>, exp<frequency, -1, 2>> {};
struct amplitude_spectral_density
: make_dimension_t<exp<base_dim_mass, 1>, exp<base_dim_length, 2>, exp<base_dim_time, -3>, exp<base_dim_current, -1>, exp<base_dim_time, 1, 2>> {
};
// todo: add support for derived_unit
//struct volt_per_sq_hertz : derived_unit<amplitude_spectral_density, kilogram, metre, second, ampere > {};
struct volt_per_sqrt_hertz : unit<amplitude_spectral_density> {};
}
namespace std::experimental::units {
template<> struct downcasting_traits<downcast_from<power_spectral_density>> : downcast_to<power_spectral_density> {};
template<> struct downcasting_traits<downcast_from<sq_volt_per_hertz>> : downcast_to<sq_volt_per_hertz> {};
template<> struct downcasting_traits<downcast_from<amplitude_spectral_density>> : downcast_to<amplitude_spectral_density> {};
template<> struct downcasting_traits<downcast_from<volt_per_sqrt_hertz>> : downcast_to<volt_per_sqrt_hertz> {};
}
namespace {
//static_assert(sqrt(quantity<sq_volt_per_hertz>(4)) = quantity<volt_per_sqrt_hertz>(2));
}

View File

@@ -44,6 +44,9 @@ namespace {
static_assert(std::is_same_v<make_dimension_t<exp<d1, 1>, exp<d0, 1>>, dimension<exp<d0, 1>, exp<d1, 1>>>);
static_assert(std::is_same_v<make_dimension_t<exp<d1, 1>, exp<d1, 1>>, dimension<exp<d1, 2>>>);
static_assert(std::is_same_v<make_dimension_t<exp<d1, 1>, exp<d1, -1>>, dimension<>>);
static_assert(std::is_same_v<make_dimension_t<exp<d1, 1>, exp<d1, 1, 2>>, dimension<exp<d1, 3, 2>>>);
static_assert(std::is_same_v<make_dimension_t<exp<d1, 1, 2>, exp<d1, 1, 2>>, dimension<exp<d1, 1>>>);
static_assert(std::is_same_v<make_dimension_t<exp<d1, 2>, exp<d1, 1, 2>>, dimension<exp<d1, 5, 2>>>);
static_assert(std::is_same_v<make_dimension_t<exp<d0, 1>, exp<d1, 1>, exp<d0, 1>, exp<d1, 1>>, dimension<exp<d0, 2>, exp<d1, 2>>>);
static_assert(