feat!: 💥 dimensionless quantities refactored
Dimensionless quantities are now represented by quantity types rather than by plain representation types. Only dimensionless quantities with `unitless` unit are implicitly convertible from representation types. `units::exp()` now is a function doing std::exp() on a representation type (previous `units::exp` class template was renamed to `units::exponent`). BREAKING_CHANGE: gcc-9.3 support removed BREAKING_CHANGE: `exp` and `Exp` renamed to `exponent` and `Exponent` Resolves #27 Resolves #42
This commit is contained in:
1 parent
c8b60b80c1
commit
563b358d5e
47 files changed
+599
-422
No files matched your search
@@ -23,7 +23,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <units/base_dimension.h>
|
||||
#include <units/exp.h>
|
||||
#include <units/exponent.h>
|
||||
#include <units/ratio.h>
|
||||
|
||||
namespace units::detail {
|
||||
@@ -45,7 +45,7 @@ constexpr ratio exp_ratio()
|
||||
template<typename... Es>
|
||||
constexpr ratio base_units_ratio(exp_list<Es...>)
|
||||
{
|
||||
return (exp_ratio<Es>() * ...);
|
||||
return (exp_ratio<Es>() * ... * ratio(1));
|
||||
}
|
||||
|
||||
} // namespace units::detail
|
||||
@@ -77,7 +77,7 @@ constexpr auto exp_text()
|
||||
}
|
||||
|
||||
template<typename... Es>
|
||||
inline constexpr int negative_exp_count = ((Es::num < 0 ? 1 : 0) + ...);
|
||||
inline constexpr int negative_exp_count = ((Es::num < 0 ? 1 : 0) + ... + 0);
|
||||
|
||||
template<typename... Us, typename... Es, std::size_t... Idxs>
|
||||
constexpr auto deduced_symbol_text(exp_list<Es...>, std::index_sequence<Idxs...>)
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
#include <units/base_dimension.h>
|
||||
#include <units/bits/external/downcasting.h>
|
||||
#include <units/exp.h>
|
||||
#include <units/exponent.h>
|
||||
|
||||
namespace units::detail {
|
||||
|
||||
@@ -35,19 +35,18 @@ namespace units::detail {
|
||||
* quantities as a product of powers of factors corresponding to the base quantities, omitting any numerical factors.
|
||||
* A power of a factor is the factor raised to an exponent.
|
||||
*
|
||||
* A derived dimension can be formed from multiple exponents (i.e. speed is represented as "exp<L, 1>, exp<T, -1>").
|
||||
* A derived dimension can be formed from multiple exponents (i.e. speed is represented as "exponent<L, 1>, exponent<T, -1>").
|
||||
* It is also possible to form a derived dimension with only one exponent (i.e. frequency is represented as just
|
||||
* "exp<T, -1>").
|
||||
* "exponent<T, -1>").
|
||||
*
|
||||
* @note This class template is used by the library engine and should not be directly instantiated by the user.
|
||||
*
|
||||
* @tparam E a first exponent of a derived dimension
|
||||
* @tparam ERest zero or more following exponents of a derived dimension
|
||||
* @tparam Es zero or more exponents of a derived dimension
|
||||
*/
|
||||
template<Exponent E, Exponent... ERest>
|
||||
requires (BaseDimension<typename E::dimension> && ... && BaseDimension<typename ERest::dimension>)
|
||||
struct derived_dimension_base : downcast_base<derived_dimension_base<E, ERest...>> {
|
||||
using exponents = exp_list<E, ERest...>;
|
||||
template<Exponent... Es>
|
||||
requires (BaseDimension<typename Es::dimension> && ...)
|
||||
struct derived_dimension_base : downcast_base<derived_dimension_base<Es...>> {
|
||||
using exponents = exp_list<Es...>;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/external/type_list.h>
|
||||
#include <units/exp.h>
|
||||
#include <units/exponent.h>
|
||||
#include <ratio> // TODO remove this dependency with #11
|
||||
|
||||
namespace units::detail {
|
||||
@@ -55,13 +55,13 @@ struct dim_consolidate<exp_list<E1, ERest...>> {
|
||||
};
|
||||
|
||||
template<BaseDimension Dim, std::intmax_t Num1, std::intmax_t Den1, std::intmax_t Num2, std::intmax_t Den2, typename... ERest>
|
||||
struct dim_consolidate<exp_list<exp<Dim, Num1, Den1>, exp<Dim, Num2, Den2>, ERest...>> {
|
||||
struct dim_consolidate<exp_list<exponent<Dim, Num1, Den1>, exponent<Dim, Num2, Den2>, ERest...>> {
|
||||
// TODO: we have ration_add now, but dim_consolidate etc, now need to cope with our new ratio
|
||||
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, typename dim_consolidate<exp_list<ERest...>>::type,
|
||||
typename dim_consolidate<exp_list<exp<Dim, r::num, r::den>, ERest...>>::type>;
|
||||
typename dim_consolidate<exp_list<exponent<Dim, r::num, r::den>, ERest...>>::type>;
|
||||
};
|
||||
|
||||
} // namespace units::detail
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
#include <units/bits/derived_dimension_base.h>
|
||||
#include <units/bits/external/type_list.h>
|
||||
#include <units/exp.h>
|
||||
#include <units/exponent.h>
|
||||
|
||||
namespace units::detail {
|
||||
|
||||
@@ -42,17 +42,17 @@ struct dim_unpack<> {
|
||||
};
|
||||
|
||||
template<BaseDimension Dim, std::intmax_t Num, std::intmax_t Den, Exponent... ERest>
|
||||
struct dim_unpack<exp<Dim, Num, Den>, ERest...> {
|
||||
using type = type_list_push_front<typename dim_unpack<ERest...>::type, exp<Dim, Num, Den>>;
|
||||
struct dim_unpack<exponent<Dim, Num, Den>, ERest...> {
|
||||
using type = type_list_push_front<typename dim_unpack<ERest...>::type, exponent<Dim, Num, Den>>;
|
||||
};
|
||||
|
||||
template<DerivedDimension Dim, std::intmax_t Num, std::intmax_t Den, Exponent... ERest>
|
||||
struct dim_unpack<exp<Dim, Num, Den>, ERest...> {
|
||||
using type = TYPENAME dim_unpack<exp<downcast_base_t<Dim>, Num, Den>, ERest...>::type;
|
||||
struct dim_unpack<exponent<Dim, Num, Den>, ERest...> {
|
||||
using type = TYPENAME dim_unpack<exponent<downcast_base_t<Dim>, Num, Den>, ERest...>::type;
|
||||
};
|
||||
|
||||
template<Exponent... Es, std::intmax_t Num, std::intmax_t Den, Exponent... ERest>
|
||||
struct dim_unpack<exp<derived_dimension_base<Es...>, Num, Den>, ERest...> {
|
||||
struct dim_unpack<exponent<derived_dimension_base<Es...>, Num, Den>, ERest...> {
|
||||
using type = type_list_push_front<typename dim_unpack<ERest...>::type, exp_multiply<Es, Num, Den>...>;
|
||||
};
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ template<Exponent E1, Exponent E2>
|
||||
struct equivalent_exp : std::false_type {};
|
||||
|
||||
template<BaseDimension Dim1, std::intmax_t Num, std::intmax_t Den, BaseDimension Dim2>
|
||||
struct equivalent_exp<exp<Dim1, Num, Den>, exp<Dim2, Num, Den>> : equivalent_dim_impl<Dim1, Dim2> {};
|
||||
struct equivalent_exp<exponent<Dim1, Num, Den>, exponent<Dim2, Num, Den>> : equivalent_dim_impl<Dim1, Dim2> {};
|
||||
|
||||
template<DerivedDimension D1, DerivedDimension D2>
|
||||
struct equivalent_derived_dim : std::false_type {};
|
||||
@@ -68,11 +68,10 @@ inline constexpr bool equivalent_dim = detail::equivalent_dim_impl<D1, D2>::valu
|
||||
* dimension. In such a case an `unknown_dimension` is created with a coherent unit of `unknown_coherent_unit`
|
||||
* and ratio(1).
|
||||
*
|
||||
* @tparam E the list of exponents of ingredient dimensions
|
||||
* @tparam ERest the list of exponents of ingredient dimensions
|
||||
* @tparam Es the list of exponents of ingredient dimensions
|
||||
*/
|
||||
template<Exponent E, Exponent... ERest>
|
||||
struct unknown_dimension : derived_dimension<unknown_dimension<E, ERest...>, unknown_coherent_unit, E, ERest...> {};
|
||||
template<Exponent... Es>
|
||||
struct unknown_dimension : derived_dimension<unknown_dimension<Es...>, unknown_coherent_unit, Es...> {};
|
||||
|
||||
namespace detail {
|
||||
|
||||
@@ -113,11 +112,11 @@ struct dim_invert_impl;
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dim_invert_impl<D> {
|
||||
using type = downcast_dimension<derived_dimension_base<exp<D, -1>>>;
|
||||
using type = downcast_dimension<derived_dimension_base<exponent<D, -1>>>;
|
||||
};
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dim_invert_impl<derived_dimension_base<exp<D, -1>>> {
|
||||
struct dim_invert_impl<derived_dimension_base<exponent<D, -1>>> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
@@ -147,7 +146,7 @@ struct to_dimension<exp_list<Es...>> {
|
||||
};
|
||||
|
||||
template<BaseDimension D>
|
||||
struct to_dimension<exp_list<exp<D, 1>>> {
|
||||
struct to_dimension<exp_list<exponent<D, 1>>> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
@@ -168,12 +167,12 @@ struct dimension_multiply_impl;
|
||||
|
||||
template<BaseDimension D1, BaseDimension D2>
|
||||
struct dimension_multiply_impl<D1, D2> {
|
||||
using type = downcast_dimension<merge_dimension<derived_dimension_base<exp<D1, 1>>, derived_dimension_base<exp<D2, 1>>>>;
|
||||
using type = downcast_dimension<merge_dimension<derived_dimension_base<exponent<D1, 1>>, derived_dimension_base<exponent<D2, 1>>>>;
|
||||
};
|
||||
|
||||
template<BaseDimension D1, DerivedDimension D2>
|
||||
struct dimension_multiply_impl<D1, D2> {
|
||||
using type = downcast_dimension<merge_dimension<derived_dimension_base<exp<D1, 1>>, typename D2::downcast_base_type>>;
|
||||
using type = downcast_dimension<merge_dimension<derived_dimension_base<exponent<D1, 1>>, typename D2::downcast_base_type>>;
|
||||
};
|
||||
|
||||
template<DerivedDimension D1, BaseDimension D2>
|
||||
@@ -202,11 +201,11 @@ struct dimension_sqrt_impl;
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dimension_sqrt_impl<D> {
|
||||
using type = downcast_dimension<derived_dimension_base<exp<D, 1, 2>>>;
|
||||
using type = downcast_dimension<derived_dimension_base<exponent<D, 1, 2>>>;
|
||||
};
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dimension_sqrt_impl<derived_dimension_base<exp<D, 2>>> {
|
||||
struct dimension_sqrt_impl<derived_dimension_base<exponent<D, 2>>> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
@@ -233,7 +232,7 @@ struct dimension_pow_impl;
|
||||
|
||||
template<BaseDimension D, std::intmax_t N>
|
||||
struct dimension_pow_impl<D, N> {
|
||||
using type = downcast_dimension<derived_dimension_base<exp<D, N>>>;
|
||||
using type = downcast_dimension<derived_dimension_base<exponent<D, N>>>;
|
||||
};
|
||||
|
||||
template<BaseDimension D>
|
||||
@@ -242,7 +241,7 @@ struct dimension_pow_impl<D, 1> {
|
||||
};
|
||||
|
||||
template<BaseDimension D, std::intmax_t N>
|
||||
struct dimension_pow_impl<derived_dimension_base<exp<D, 1, N>>, N> {
|
||||
struct dimension_pow_impl<derived_dimension_base<exponent<D, 1, N>>, N> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
|
||||
@@ -36,28 +36,28 @@ template<ratio R>
|
||||
constexpr auto ratio_text()
|
||||
{
|
||||
if constexpr(R.num == 1 && R.den == 1 && R.exp != 0) {
|
||||
return base_multiplier + superscript<R.exp>() + basic_fixed_string(" ");
|
||||
return base_multiplier + superscript<R.exp>();
|
||||
}
|
||||
else if constexpr(R.num != 1 || R.den != 1 || R.exp != 0) {
|
||||
auto txt = basic_fixed_string("[") + regular<R.num>();
|
||||
if constexpr(R.den == 1) {
|
||||
if constexpr(R.exp == 0) {
|
||||
return txt + basic_fixed_string("] ");
|
||||
return txt + basic_fixed_string("]");
|
||||
}
|
||||
else {
|
||||
return txt + " " + base_multiplier + superscript<R.exp>() +
|
||||
basic_fixed_string("] ");
|
||||
basic_fixed_string("]");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if constexpr(R.exp == 0) {
|
||||
return txt + basic_fixed_string("/") + regular<R.den>() +
|
||||
basic_fixed_string("] ");
|
||||
basic_fixed_string("]");
|
||||
}
|
||||
else {
|
||||
return txt + basic_fixed_string("/") + regular<R.den>() +
|
||||
" " + base_multiplier + superscript<R.exp>() +
|
||||
basic_fixed_string("] ");
|
||||
basic_fixed_string("]");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ constexpr auto ratio_text()
|
||||
}
|
||||
}
|
||||
|
||||
template<ratio R, typename PrefixFamily>
|
||||
template<ratio R, typename PrefixFamily, std::size_t SymbolLen>
|
||||
constexpr auto prefix_or_ratio_text()
|
||||
{
|
||||
if constexpr(R.num == 1 && R.den == 1 && R.exp == 0) {
|
||||
@@ -84,12 +84,20 @@ constexpr auto prefix_or_ratio_text()
|
||||
}
|
||||
else {
|
||||
// print as a ratio of the coherent unit
|
||||
return ratio_text<R>();
|
||||
constexpr auto txt = ratio_text<R>();
|
||||
if constexpr(SymbolLen > 0 && txt.standard().size() > 0)
|
||||
return txt + basic_fixed_string(" ");
|
||||
else
|
||||
return txt;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// print as a ratio of the coherent unit
|
||||
return ratio_text<R>();
|
||||
constexpr auto txt = ratio_text<R>();
|
||||
if constexpr(SymbolLen > 0 && txt.standard().size() > 0)
|
||||
return txt + basic_fixed_string(" ");
|
||||
else
|
||||
return txt;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,8 +105,7 @@ constexpr auto prefix_or_ratio_text()
|
||||
template<typename... Es, std::size_t... Idxs>
|
||||
constexpr auto derived_dimension_unit_text(exp_list<Es...>, std::index_sequence<Idxs...>)
|
||||
{
|
||||
constexpr auto neg_exp = negative_exp_count<Es...>;
|
||||
return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, neg_exp, Idxs>() + ...);
|
||||
return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, negative_exp_count<Es...>, Idxs>() + ... + basic_symbol_text(basic_fixed_string("")));
|
||||
}
|
||||
|
||||
template<typename... Es>
|
||||
@@ -129,6 +136,11 @@ constexpr auto exp_list_with_named_units(exp_list<Es...>)
|
||||
return type_list_join<decltype(exp_list_with_named_units(Es()))...>();
|
||||
}
|
||||
|
||||
constexpr auto exp_list_with_named_units(exp_list<> empty)
|
||||
{
|
||||
return empty;
|
||||
}
|
||||
|
||||
template<Dimension Dim>
|
||||
constexpr auto derived_dimension_unit_text()
|
||||
{
|
||||
@@ -150,15 +162,18 @@ constexpr auto unit_text()
|
||||
else {
|
||||
// print as a prefix or ratio of a coherent unit
|
||||
using coherent_unit = dimension_unit<Dim>;
|
||||
auto prefix_txt = prefix_or_ratio_text<U::ratio / coherent_unit::ratio, typename U::reference::prefix_family>();
|
||||
|
||||
if constexpr(has_symbol<coherent_unit>) {
|
||||
// use predefined coherent unit symbol
|
||||
return prefix_txt + coherent_unit::symbol;
|
||||
constexpr auto symbol_text = coherent_unit::symbol;
|
||||
constexpr auto prefix_txt = prefix_or_ratio_text<U::ratio / coherent_unit::ratio, typename U::reference::prefix_family, symbol_text.standard().size()>();
|
||||
return prefix_txt + symbol_text;
|
||||
}
|
||||
else {
|
||||
// use derived dimension ingredients to create a unit symbol
|
||||
return prefix_txt + derived_dimension_unit_text<Dim>();
|
||||
constexpr auto symbol_text = derived_dimension_unit_text<Dim>();
|
||||
constexpr auto prefix_txt = prefix_or_ratio_text<U::ratio / coherent_unit::ratio, typename U::reference::prefix_family, symbol_text.standard().size()>();
|
||||
return prefix_txt + symbol_text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user