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:
Mateusz Pusz
2020-09-08 11:02:16 +02:00
parent c8b60b80c1
commit 563b358d5e
47 changed files with 599 additions and 422 deletions
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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>
+3 -3
View File
@@ -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
+6 -6
View File
@@ -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>...>;
};
+13 -14
View File
@@ -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;
};
+28 -13
View File
@@ -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;
}
}
}
+5 -5
View File
@@ -128,23 +128,23 @@ concept BaseDimension = detail::is_derived_from_base_dimension<T>;
namespace detail {
template<typename T>
inline constexpr bool is_exp = false;
inline constexpr bool is_exponent = false;
} // namespace detail
/**
* @brief A concept matching dimension's exponents.
*
* Satisfied by all specializations of :class:`exp`.
* Satisfied by all specializations of :class:`exponent`.
*/
template<typename T>
concept Exponent = detail::is_exp<T>;
concept Exponent = detail::is_exponent<T>;
// DerivedDimension
namespace detail {
template<Exponent E, Exponent... ERest>
requires (BaseDimension<typename E::dimension> && ... && BaseDimension<typename ERest::dimension>)
template<Exponent... Es>
requires (BaseDimension<typename Es::dimension> && ...)
struct derived_dimension_base;
} // namespace detail
+1 -1
View File
@@ -30,7 +30,7 @@
namespace units::data {
struct bit_per_second : unit<bit_per_second> {};
struct dim_bitrate : derived_dimension<dim_bitrate, bit_per_second, exp<dim_information, 1>, exp<physical::si::dim_time, -1>> {};
struct dim_bitrate : derived_dimension<dim_bitrate, bit_per_second, exponent<dim_information, 1>, exponent<physical::si::dim_time, -1>> {};
struct kibibit_per_second : deduced_unit<kibibit_per_second, dim_bitrate, kibibit, physical::si::second> {};
struct mebibit_per_second : deduced_unit<mebibit_per_second, dim_bitrate, mebibit, physical::si::second> {};
+5 -6
View File
@@ -29,7 +29,7 @@
#include <units/bits/dim_unpack.h>
#include <units/bits/external/downcasting.h>
#include <units/bits/external/type_list.h>
#include <units/exp.h>
#include <units/exponent.h>
namespace units {
@@ -75,12 +75,11 @@ using make_dimension = TYPENAME to_derived_dimension_base<typename dim_consolida
*
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
* @tparam U a coherent unit of a derived dimension
* @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<typename Child, Unit U, Exponent E, Exponent... ERest>
struct derived_dimension : downcast_child<Child, typename detail::make_dimension<E, ERest...>> {
using recipe = exp_list<E, ERest...>;
template<typename Child, Unit U, Exponent... Es>
struct derived_dimension : downcast_child<Child, typename detail::make_dimension<Es...>> {
using recipe = exp_list<Es...>;
using coherent_unit = U;
static constexpr ratio base_units_ratio = detail::base_units_ratio(typename derived_dimension::exponents());
};
+46
View File
@@ -0,0 +1,46 @@
// 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.
#pragma once
#include <units/quantity_cast.h>
namespace units {
struct unitless : named_unit<unitless, "", no_prefix> {};
struct percent : named_scaled_unit<percent, "%", no_prefix, ratio(1, 100), unitless> {};
/**
* @brief Dimension one
*
* Dimension for which all the exponents of the factors corresponding to the base
* dimensions are zero. Also commonly named as "dimensionless".
*/
struct dim_one : derived_dimension<dim_one, unitless> {};
template<typename T>
concept Dimensionless = QuantityOf<T, dim_one>;
template<Unit U, Scalar Rep = double>
using dimensionless = quantity<dim_one, U, Rep>;
} // namespace units
@@ -35,17 +35,17 @@ namespace units {
* @tparam Den denominator of the factor
*/
template<Dimension Dim, std::intmax_t Num, std::intmax_t Den = 1>
struct exp {
struct exponent {
using dimension = Dim;
static constexpr int num = Num;
static constexpr int den = Den;
};
// is_exp
// is_exponent
namespace detail {
template<typename Dim, std::intmax_t Num, std::intmax_t Den>
inline constexpr bool is_exp<exp<Dim, Num, Den>> = true;
inline constexpr bool is_exponent<exponent<Dim, Num, Den>> = true;
} // namespace detail
@@ -58,7 +58,7 @@ struct exp_less : base_dimension_less<typename E1::dimension, typename E2::dimen
namespace detail {
template<typename Dim, std::intmax_t Num, std::intmax_t Den>
constexpr exp<Dim, -Num, Den> exp_invert_impl(exp<Dim, Num, Den>);
constexpr exponent<Dim, -Num, Den> exp_invert_impl(exponent<Dim, Num, Den>);
} // namespace detail
@@ -71,7 +71,7 @@ namespace detail {
template<Exponent E, std::intmax_t Num, std::intmax_t Den>
struct exp_multiply_impl {
static constexpr ratio r = ratio(E::num, E::den) * ratio(Num, Den);
using type = exp<typename E::dimension, r.num, r.den>;
using type = exponent<typename E::dimension, r.num, r.den>;
};
} // namespace detail
+14 -1
View File
@@ -79,6 +79,19 @@ inline Quantity AUTO sqrt(const Q& q) noexcept
return quantity<dim, unit, rep>(static_cast<rep>(std::sqrt(q.count())));
}
/**
* @brief Computes Euler's raised to the given power
*
* @param q Quantity being the base of the operation
* @return Quantity The value of the same quantity type
*/
template<typename D, typename U, typename Rep>
inline quantity<D, U, Rep> exp(const quantity<D, U, Rep>& q)
{
using coherent_unit = dimension_unit<D>;
return quantity_cast<U>(quantity<D, coherent_unit, Rep>(std::exp(quantity_cast<coherent_unit>(q).count())));
}
/**
* @brief Computes the absolute value of a quantity
*
@@ -86,7 +99,7 @@ inline Quantity AUTO sqrt(const Q& q) noexcept
* @return Quantity The absolute value of a provided quantity
*/
template<Quantity Q>
constexpr Quantity AUTO abs(const Q& q) noexcept
inline Quantity AUTO abs(const Q& q) noexcept
requires requires { std::abs(q.count()); }
{
return Q(std::abs(q.count()));
+40 -40
View File
@@ -65,126 +65,126 @@ struct dim_angle : base_dimension<"A", U> {};
template<typename Child, Unit U, DimensionOf<dim_angle> A, DimensionOf<dim_time> T>
struct dim_angular_velocity : derived_dimension<Child, U, exp<A, 1>, exp<T, -1>> {};
struct dim_angular_velocity : derived_dimension<Child, U, exponent<A, 1>, exponent<T, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_time> T>
struct dim_frequency : derived_dimension<Child, U, exp<T, -1>> {};
struct dim_frequency : derived_dimension<Child, U, exponent<T, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_length> L>
struct dim_area : derived_dimension<Child, U, exp<L, 2>> {};
struct dim_area : derived_dimension<Child, U, exponent<L, 2>> {};
template<typename Child, Unit U, DimensionOf<dim_length> L>
struct dim_volume : derived_dimension<Child, U, exp<L, 3>> {};
struct dim_volume : derived_dimension<Child, U, exponent<L, 3>> {};
template<typename Child, Unit U, DimensionOf<dim_length> L, DimensionOf<dim_time> T>
struct dim_speed : derived_dimension<Child, U, exp<L, 1>, exp<T, -1>> {};
struct dim_speed : derived_dimension<Child, U, exponent<L, 1>, exponent<T, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_length> L, DimensionOf<dim_time> T>
struct dim_acceleration : derived_dimension<Child, U, exp<L, 1>, exp<T, -2>> {};
struct dim_acceleration : derived_dimension<Child, U, exponent<L, 1>, exponent<T, -2>> {};
template<typename Child, Unit U, DimensionOf<dim_mass> M, DimensionOf<dim_acceleration> A>
struct dim_force : derived_dimension<Child, U, exp<M, 1>, exp<A, 1>> {};
struct dim_force : derived_dimension<Child, U, exponent<M, 1>, exponent<A, 1>> {};
template<typename Child, Unit U, DimensionOf<dim_mass> M, DimensionOf<dim_speed> V>
struct dim_momentum : derived_dimension<Child, U, exp<M, 1>, exp<V, 1>> {};
struct dim_momentum : derived_dimension<Child, U, exponent<M, 1>, exponent<V, 1>> {};
template<typename Child, Unit U, DimensionOf<dim_force> F, DimensionOf<dim_length> L>
struct dim_energy : derived_dimension<Child, U, exp<F, 1>, exp<L, 1>> {};
struct dim_energy : derived_dimension<Child, U, exponent<F, 1>, exponent<L, 1>> {};
template<typename Child, Unit U, DimensionOf<dim_energy> E, DimensionOf<dim_angle> A>
struct dim_torque : derived_dimension<Child, U, exp<E, 1>, exp<A, 1>> {};
struct dim_torque : derived_dimension<Child, U, exponent<E, 1>, exponent<A, 1>> {};
template<typename Child, Unit U, DimensionOf<dim_mass> M, DimensionOf<dim_length> L>
struct dim_density : derived_dimension<Child, U, exp<M, 1>, exp<L, -3>> {};
struct dim_density : derived_dimension<Child, U, exponent<M, 1>, exponent<L, -3>> {};
template<typename Child, Unit U, DimensionOf<dim_energy> E, DimensionOf<dim_time> T>
struct dim_power : derived_dimension<Child, U, exp<E, 1>, exp<T, -1>> {};
struct dim_power : derived_dimension<Child, U, exponent<E, 1>, exponent<T, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_power> P, DimensionOf<dim_electric_current> C>
struct dim_voltage : derived_dimension<Child, U, exp<P, 1>, exp<C, -1>> {};
struct dim_voltage : derived_dimension<Child, U, exponent<P, 1>, exponent<C, -1>> {};
template <typename Child, Unit U, DimensionOf<dim_voltage> V, DimensionOf<dim_electric_current> C>
struct dim_resistance : derived_dimension<Child,U, exp<V, 1>, exp<C, -1>> {};
struct dim_resistance : derived_dimension<Child,U, exponent<V, 1>, exponent<C, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_time> T, DimensionOf<dim_electric_current> C>
struct dim_electric_charge : derived_dimension<Child, U, exp<T, 1>, exp<C, 1>> {};
struct dim_electric_charge : derived_dimension<Child, U, exponent<T, 1>, exponent<C, 1>> {};
template<typename Child, Unit U, DimensionOf<dim_electric_charge> C, DimensionOf<dim_voltage> V>
struct dim_capacitance : derived_dimension<Child, U, exp<C, 1>, exp<V, -1>> {};
struct dim_capacitance : derived_dimension<Child, U, exponent<C, 1>, exponent<V, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_force> F, DimensionOf<dim_length> L>
struct dim_surface_tension : derived_dimension<Child, U, exp<F, 1>, exp<L, -1>> {};
struct dim_surface_tension : derived_dimension<Child, U, exponent<F, 1>, exponent<L, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_force> F, DimensionOf<dim_area> A>
struct dim_pressure : derived_dimension<Child, U, exp<F, 1>, exp<A, -1>> {};
struct dim_pressure : derived_dimension<Child, U, exponent<F, 1>, exponent<A, -1>> {};
template <typename Child, Unit U, DimensionOf<dim_voltage> V, DimensionOf<dim_time> T, DimensionOf<dim_length> L>
struct dim_magnetic_induction : derived_dimension<Child, U, exp<V, 1>, exp<T, 1>, exp<L, -2>> {};
struct dim_magnetic_induction : derived_dimension<Child, U, exponent<V, 1>, exponent<T, 1>, exponent<L, -2>> {};
template<typename Child, Unit U, DimensionOf<dim_magnetic_induction> B, DimensionOf<dim_area> A>
struct dim_magnetic_flux : derived_dimension<Child, U, exp<B, 1>, exp<A, 1>> {};
struct dim_magnetic_flux : derived_dimension<Child, U, exponent<B, 1>, exponent<A, 1>> {};
template<typename Child, Unit U, DimensionOf<dim_magnetic_flux> F, DimensionOf<dim_electric_current> I>
struct dim_inductance : derived_dimension<Child, U, exp<F, 1>, exp<I, -1>> {};
struct dim_inductance : derived_dimension<Child, U, exponent<F, 1>, exponent<I, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_resistance> R>
struct dim_conductance : derived_dimension<Child, U, exp<R, -1>> {};
struct dim_conductance : derived_dimension<Child, U, exponent<R, -1>> {};
// TODO Add when downcasting issue is solved
// template<typename Child, Unit U, DimensionOf<dim_time> T>
// struct dim_radioactivity : derived_dimension<Child, U, exp<T, -1>> {};
// struct dim_radioactivity : derived_dimension<Child, U, exponent<T, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_time> T, DimensionOf<dim_substance> M>
struct dim_catalytic_activity : derived_dimension<Child, U, exp<T, -1>, exp<M, 1>> {};
struct dim_catalytic_activity : derived_dimension<Child, U, exponent<T, -1>, exponent<M, 1>> {};
template<typename Child, Unit U, DimensionOf<dim_energy> E, DimensionOf<dim_mass> M>
struct dim_absorbed_dose : derived_dimension<Child, U, exp<E, 1>, exp<M, -1>> {};
struct dim_absorbed_dose : derived_dimension<Child, U, exponent<E, 1>, exponent<M, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_electric_current> I, DimensionOf<dim_length> L>
struct dim_current_density : derived_dimension<Child, U, exp<I, 1>, exp<L, -2>> {};
struct dim_current_density : derived_dimension<Child, U, exponent<I, 1>, exponent<L, -2>> {};
template<typename Child, Unit U, DimensionOf<dim_substance> M, DimensionOf<dim_length> L>
struct dim_concentration : derived_dimension<Child, U, exp<M, 1>, exp<L, -3>> {};
struct dim_concentration : derived_dimension<Child, U, exponent<M, 1>, exponent<L, -3>> {};
template<typename Child, Unit U, DimensionOf<dim_luminous_intensity> I, DimensionOf<dim_length> L>
struct dim_luminance : derived_dimension<Child, U, exp<I, 1>, exp<L, -2>> {};
struct dim_luminance : derived_dimension<Child, U, exponent<I, 1>, exponent<L, -2>> {};
template<typename Child, Unit U, DimensionOf<dim_pressure> P, DimensionOf<dim_time> T>
struct dim_dynamic_viscosity : derived_dimension<Child, U, exp<P, 1>, exp<T, 1>> {};
struct dim_dynamic_viscosity : derived_dimension<Child, U, exponent<P, 1>, exponent<T, 1>> {};
template<typename Child, Unit U, DimensionOf<dim_energy> E, DimensionOf<dim_thermodynamic_temperature> T>
struct dim_heat_capacity : derived_dimension<Child, U, exp<E, 1>, exp<T, -1>> {};
struct dim_heat_capacity : derived_dimension<Child, U, exponent<E, 1>, exponent<T, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_heat_capacity> C, DimensionOf<dim_mass> M>
struct dim_specific_heat_capacity : derived_dimension<Child, U, exp<C, 1>, exp<M, -1>> {};
struct dim_specific_heat_capacity : derived_dimension<Child, U, exponent<C, 1>, exponent<M, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_heat_capacity> C, DimensionOf<dim_substance> M>
struct dim_molar_heat_capacity : derived_dimension<Child, U, exp<C, 1>, exp<M, -1>> {};
struct dim_molar_heat_capacity : derived_dimension<Child, U, exponent<C, 1>, exponent<M, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_power> P, DimensionOf<dim_length> L, DimensionOf<dim_thermodynamic_temperature> T>
struct dim_thermal_conductivity : derived_dimension<Child, U, exp<P, 1>, exp<L, -1>, exp<T, -1>> {};
struct dim_thermal_conductivity : derived_dimension<Child, U, exponent<P, 1>, exponent<L, -1>, exponent<T, -1>> {};
// TODO Add when downcasting issue is solved
// template<typename Child, Unit U, DimensionOf<dim_energy> E, DimensionOf<dim_length> L>
// struct dim_energy_density : derived_dimension<Child, U, exp<E, 1>, exp<L, -3>> {};
// struct dim_energy_density : derived_dimension<Child, U, exponent<E, 1>, exponent<L, -3>> {};
template<typename Child, Unit U, DimensionOf<dim_voltage> V, DimensionOf<dim_length> L>
struct dim_electric_field_strength : derived_dimension<Child, U, exp<V, 1>, exp<L, -1>> {};
struct dim_electric_field_strength : derived_dimension<Child, U, exponent<V, 1>, exponent<L, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_electric_charge> Q, DimensionOf<dim_length> L>
struct dim_charge_density : derived_dimension<Child, U, exp<Q, 1>, exp<L, -3>> {};
struct dim_charge_density : derived_dimension<Child, U, exponent<Q, 1>, exponent<L, -3>> {};
template<typename Child, Unit U, DimensionOf<dim_electric_charge> Q, DimensionOf<dim_length> L>
struct dim_surface_charge_density : derived_dimension<Child, U, exp<Q, 1>, exp<L, -2>> {};
struct dim_surface_charge_density : derived_dimension<Child, U, exponent<Q, 1>, exponent<L, -2>> {};
template<typename Child, Unit U, DimensionOf<dim_capacitance> C, DimensionOf<dim_length> L>
struct dim_permittivity : derived_dimension<Child, U, exp<C, 1>, exp<L, -1>> {};
struct dim_permittivity : derived_dimension<Child, U, exponent<C, 1>, exponent<L, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_inductance> H, DimensionOf<dim_length> L>
struct dim_permeability : derived_dimension<Child, U, exp<H, 1>, exp<L, -1>> {};
struct dim_permeability : derived_dimension<Child, U, exponent<H, 1>, exponent<L, -1>> {};
template<typename Child, Unit U, DimensionOf<dim_energy> E, DimensionOf<dim_substance> M>
struct dim_molar_energy : derived_dimension<Child, U, exp<E, 1>, exp<M, -1>> {};
struct dim_molar_energy : derived_dimension<Child, U, exponent<E, 1>, exponent<M, -1>> {};
template<typename T>
concept Length = QuantityOf<T, dim_length>;
+119 -131
View File
@@ -27,6 +27,7 @@
#include <units/bits/dimension_op.h>
#include <units/bits/pow.h>
#include <units/bits/to_string.h>
#include <units/dimensionless.h>
#include <units/quantity_cast.h>
#if COMP_MSVC || COMP_GCC >= 10
@@ -76,7 +77,7 @@ public:
template<Scalar Value>
requires detail::safe_convertible<Value, rep>
constexpr explicit quantity(const Value& v) : value_{static_cast<rep>(v)} {}
constexpr explicit(!(std::is_same_v<dimension, dim_one> && std::is_same_v<unit, unitless>)) quantity(const Value& v) : value_{static_cast<rep>(v)} {}
template<Quantity Q2>
requires equivalent_dim<D, typename Q2::dimension> &&
@@ -220,6 +221,123 @@ public:
// Hidden Friends
// Below friend functions are to be found via argument-dependent lookup only
[[nodiscard]] friend constexpr quantity operator+(const quantity& lhs, const quantity& rhs)
requires std::regular_invocable<std::plus<>, Rep, Rep>
{
return quantity(lhs.count() + rhs.count());
}
template<typename U2, typename Rep2>
[[nodiscard]] friend constexpr Quantity AUTO operator+(const quantity& lhs, const quantity<D, U2, Rep2>& rhs)
requires std::regular_invocable<std::plus<>, Rep, Rep2>
{
using common_rep = decltype(lhs.count() + rhs.count());
using ret = common_quantity<quantity, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() + ret(rhs).count());
}
[[nodiscard]] friend constexpr quantity operator-(const quantity& lhs, const quantity& rhs)
requires std::regular_invocable<std::minus<>, Rep, Rep>
{
return quantity(lhs.count() - rhs.count());
}
template<typename U2, typename Rep2>
[[nodiscard]] friend constexpr Quantity AUTO operator-(const quantity& lhs, const quantity<D, U2, Rep2>& rhs)
requires std::regular_invocable<std::minus<>, Rep, Rep2>
{
using common_rep = decltype(lhs.count() - rhs.count());
using ret = common_quantity<quantity, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() - ret(rhs).count());
}
template<Scalar Value>
[[nodiscard]] friend constexpr Quantity AUTO operator*(const quantity& q, const Value& v)
requires std::regular_invocable<std::multiplies<>, Rep, Value>
{
using common_rep = decltype(q.count() * v);
using ret = quantity<D, U, common_rep>;
return ret(q.count() * v);
}
template<Scalar Value>
[[nodiscard]] friend constexpr Quantity AUTO operator*(const Value& v, const quantity& q)
requires std::regular_invocable<std::multiplies<>, Value, Rep>
{
return q * v;
}
template<typename D2, typename U2, typename Rep2>
[[nodiscard]] friend constexpr Quantity AUTO operator*(const quantity& lhs, const quantity<D2, U2, Rep2>& rhs)
requires std::regular_invocable<std::multiplies<>, Rep, Rep2>
{
using dim = dimension_multiply<D, D2>;
using ret_unit = downcast_unit<dim, (U::ratio / dimension_unit<D>::ratio) * (U2::ratio / dimension_unit<D2>::ratio) * dimension_unit<dim>::ratio>;
using common_rep = decltype(lhs.count() * rhs.count());
using ret = quantity<dim, ret_unit, common_rep>;
return ret(lhs.count() * rhs.count());
}
template<Scalar Value>
[[nodiscard]] friend constexpr Quantity AUTO operator/(const Value& v, const quantity& q)
requires std::regular_invocable<std::divides<>, Value, Rep>
{
Expects(q.count() != 0);
using dim = dim_invert<D>;
using ret_unit = downcast_unit<dim, ratio(U::ratio.den, U::ratio.num, -U::ratio.exp)>;
using common_rep = decltype(v / q.count());
using ret = quantity<dim, ret_unit, common_rep>;
return ret(v / q.count());
}
template<Scalar Value>
[[nodiscard]] friend constexpr Quantity AUTO operator/(const quantity& q, const Value& v)
requires std::regular_invocable<std::divides<>, Rep, Value>
{
Expects(v != Value{0});
using common_rep = decltype(q.count() / v);
using ret = quantity<D, U, common_rep>;
return ret(q.count() / v);
}
template<typename D2, typename U2, typename Rep2>
[[nodiscard]] friend constexpr Quantity AUTO operator/(const quantity& lhs, const quantity<D2, U2, Rep2>& rhs)
requires std::regular_invocable<std::divides<>, Rep, Rep2>
{
Expects(rhs.count() != 0);
using common_rep = decltype(lhs.count() / rhs.count());
using dim = dimension_divide<D, D2>;
using ret_unit = downcast_unit<dim, (U::ratio / dimension_unit<D>::ratio) / (U2::ratio / dimension_unit<D2>::ratio) * dimension_unit<dim>::ratio>;
using ret = quantity<dim, ret_unit, common_rep>;
return ret(lhs.count() / rhs.count());
}
template<Scalar Value>
[[nodiscard]] friend constexpr Quantity AUTO operator%(const quantity& q, const Value& v)
requires (!treat_as_floating_point<Rep>) &&
(!treat_as_floating_point<Value>) &&
std::regular_invocable<std::modulus<>, Rep, Value>
{
using common_rep = decltype(q.count() % v);
using ret = quantity<D, U, common_rep>;
return ret(q.count() % v);
}
template<typename U2, typename Rep2>
[[nodiscard]] friend constexpr Quantity AUTO operator%(const quantity& lhs, const quantity<D, U2, Rep2>& rhs)
requires (!treat_as_floating_point<Rep>) &&
(!treat_as_floating_point<Rep2>) &&
std::regular_invocable<std::modulus<>, Rep, Rep2>
{
using common_rep = decltype(lhs.count() % rhs.count());
using ret = common_quantity<quantity, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() % ret(rhs).count());
}
#if COMP_MSVC || COMP_GCC >= 10
template<typename D2, typename U2, typename Rep2>
@@ -301,136 +419,6 @@ public:
}
};
template<typename D, typename U1, typename Rep1, typename U2, typename Rep2>
[[nodiscard]] constexpr Quantity AUTO operator+(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
requires std::regular_invocable<std::plus<>, Rep1, Rep2>
{
using common_rep = decltype(lhs.count() + rhs.count());
using ret = common_quantity<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() + ret(rhs).count());
}
template<typename D, typename U1, typename Rep1, typename U2, typename Rep2>
[[nodiscard]] constexpr Quantity AUTO operator-(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
requires std::regular_invocable<std::minus<>, Rep1, Rep2>
{
using common_rep = decltype(lhs.count() - rhs.count());
using ret = common_quantity<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() - ret(rhs).count());
}
template<typename D, typename U, typename Rep, Scalar Value>
[[nodiscard]] constexpr Quantity AUTO operator*(const quantity<D, U, Rep>& q, const Value& v)
requires std::regular_invocable<std::multiplies<>, Rep, Value>
{
using common_rep = decltype(q.count() * v);
using ret = quantity<D, U, common_rep>;
return ret(q.count() * v);
}
template<Scalar Value, typename D, typename U, typename Rep>
[[nodiscard]] constexpr Quantity AUTO operator*(const Value& v, const quantity<D, U, Rep>& q)
requires std::regular_invocable<std::multiplies<>, Value, Rep>
{
return q * v;
}
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
[[nodiscard]] constexpr Scalar AUTO operator*(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
requires std::regular_invocable<std::multiplies<>, Rep1, Rep2> &&
equivalent_dim<D1, dim_invert<D2>>
{
using common_rep = decltype(lhs.count() * rhs.count());
const ratio r = U1::ratio * U2::ratio;
if constexpr (treat_as_floating_point<common_rep>) {
return lhs.count() * rhs.count() * static_cast<common_rep>(r.num * detail::fpow10<common_rep>(r.exp)) / static_cast<common_rep>(r.den);
} else {
return lhs.count() * rhs.count() * static_cast<common_rep>(r.num * detail::ipow10(r.exp)) / static_cast<common_rep>(r.den);
}
}
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
[[nodiscard]] constexpr Quantity AUTO operator*(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
requires std::regular_invocable<std::multiplies<>, Rep1, Rep2>
{
using dim = dimension_multiply<D1, D2>;
using unit = downcast_unit<dim, (U1::ratio / dimension_unit<D1>::ratio) * (U2::ratio / dimension_unit<D2>::ratio) * dimension_unit<dim>::ratio>;
using common_rep = decltype(lhs.count() * rhs.count());
using ret = quantity<dim, unit, common_rep>;
return ret(lhs.count() * rhs.count());
}
template<Scalar Value, typename D, typename U, typename Rep>
[[nodiscard]] constexpr Quantity AUTO operator/(const Value& v, const quantity<D, U, Rep>& q)
requires std::regular_invocable<std::divides<>, Value, Rep>
{
Expects(q.count() != 0);
using dim = dim_invert<D>;
using unit = downcast_unit<dim, ratio(U::ratio.den, U::ratio.num, -U::ratio.exp)>;
using common_rep = decltype(v / q.count());
using ret = quantity<dim, unit, common_rep>;
return ret(v / q.count());
}
template<typename D, typename U, typename Rep, Scalar Value>
[[nodiscard]] constexpr Quantity AUTO operator/(const quantity<D, U, Rep>& q, const Value& v)
requires std::regular_invocable<std::divides<>, Rep, Value>
{
Expects(v != Value{0});
using common_rep = decltype(q.count() / v);
using ret = quantity<D, U, common_rep>;
return ret(q.count() / v);
}
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
[[nodiscard]] constexpr Scalar AUTO operator/(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
requires std::regular_invocable<std::divides<>, Rep1, Rep2> &&
equivalent_dim<D1, D2>
{
Expects(rhs.count() != 0);
using common_rep = decltype(lhs.count() / rhs.count());
using cq = common_quantity<quantity<D1, U1, Rep1>, quantity<D2, U2, Rep2>, common_rep>;
return cq(lhs).count() / cq(rhs).count();
}
template<typename D1, typename U1, typename Rep1, typename D2, typename U2, typename Rep2>
[[nodiscard]] constexpr Quantity AUTO operator/(const quantity<D1, U1, Rep1>& lhs, const quantity<D2, U2, Rep2>& rhs)
requires std::regular_invocable<std::divides<>, Rep1, Rep2>
{
Expects(rhs.count() != 0);
using common_rep = decltype(lhs.count() / rhs.count());
using dim = dimension_divide<D1, D2>;
using unit = downcast_unit<dim, (U1::ratio / dimension_unit<D1>::ratio) / (U2::ratio / dimension_unit<D2>::ratio) * dimension_unit<dim>::ratio>;
using ret = quantity<dim, unit, common_rep>;
return ret(lhs.count() / rhs.count());
}
template<typename D, typename U, typename Rep, Scalar Value>
[[nodiscard]] constexpr Quantity AUTO operator%(const quantity<D, U, Rep>& q, const Value& v)
requires (!treat_as_floating_point<Rep>) &&
(!treat_as_floating_point<Value>) &&
std::regular_invocable<std::modulus<>, Rep, Value>
{
using common_rep = decltype(q.count() % v);
using ret = quantity<D, U, common_rep>;
return ret(q.count() % v);
}
template<typename D, typename U1, typename Rep1, typename U2, typename Rep2>
[[nodiscard]] constexpr Quantity AUTO operator%(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
requires (!treat_as_floating_point<Rep1>) &&
(!treat_as_floating_point<Rep2>) &&
std::regular_invocable<std::modulus<>, Rep1, Rep2>
{
using common_rep = decltype(lhs.count() % rhs.count());
using ret = common_quantity<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, common_rep>;
return ret(ret(lhs).count() % ret(rhs).count());
}
namespace detail {
template<typename D, typename U, typename Rep>
+8 -4
View File
@@ -27,8 +27,6 @@
#include <units/bits/dimension_op.h>
#include <units/bits/external/type_traits.h>
#include <units/bits/pow.h>
#include <units/quantity.h>
#include <units/quantity_point.h>
#include <cassert>
#ifdef _MSC_VER
@@ -38,6 +36,12 @@
namespace units {
template<Dimension D, UnitOf<D> U, Scalar Rep>
class quantity;
template<Dimension D, UnitOf<D> U, Scalar Rep>
class quantity_point;
namespace detail {
template<typename D, typename U, typename Rep>
@@ -284,8 +288,8 @@ struct quantity_cast_impl<To, CRatio, CRep, false, true, false> {
template<typename Q1, typename Q2>
constexpr ratio cast_ratio(const Q1& from, const Q2& to)
{
using FromU = Q1::unit;
using ToU = Q2::unit;
using FromU = TYPENAME Q1::unit;
using ToU = TYPENAME Q2::unit;
if constexpr(same_unit_reference<FromU, ToU>::value) {
return FromU::ratio / ToU::ratio;
}