Migrate units from ratio to Magnitude
This commit is huge, but hopefully the cognitive load is not too bad. The bulk of this commit is just some fairly mechanical updates from `ratio` to `Magnitude`. Other things to call out: - `UnitRatio` goes away. We don't need this concept, because Magnitude can't even _represent_ anything that doesn't satisfy it. - I commented out some formatting test cases where the precise expression changes, but the number is completely equivalent. We will need to decide how we want to handle Magnitude formatting as a separate, follow-on task. But at least Magnitude gives us all the tools we'll need to do so!
This commit is contained in:
1 parent
5afe7766e9
commit
d19b2803ce
42 files changed
+197
-199
No files matched your search
@@ -45,14 +45,4 @@ constexpr Magnitude auto absolute_magnitude(exponent_list<Es...>)
|
||||
return (pow<ratio{Es::num, Es::den}>(Es::dimension::base_unit::mag) * ... * magnitude<>{});
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the common ratio of all the references of base units in the derived dimension
|
||||
*/
|
||||
template<typename... Es>
|
||||
constexpr ratio base_units_ratio(exponent_list<Es...> es)
|
||||
{
|
||||
return as_ratio(absolute_magnitude(es));
|
||||
}
|
||||
|
||||
|
||||
} // namespace units::detail
|
||||
@@ -28,7 +28,7 @@
|
||||
#include <units/bits/external/fixed_string.h>
|
||||
#include <units/bits/external/type_traits.h>
|
||||
#include <units/customization_points.h>
|
||||
#include <units/ratio.h>
|
||||
#include <units/magnitude.h>
|
||||
// IWYU pragma: end_exports
|
||||
|
||||
#include <cstdint>
|
||||
@@ -67,24 +67,15 @@ void to_prefix_base(const volatile prefix_base<PF, R>*);
|
||||
template<typename T>
|
||||
concept Prefix = requires(T* t) { detail::to_prefix_base(t); };
|
||||
|
||||
/**
|
||||
* @brief A concept matching unit's ratio
|
||||
*
|
||||
* Satisfied by all ratio values for which `R.num > 0` and `R.den > 0`.
|
||||
*/
|
||||
template<ratio R>
|
||||
concept UnitRatio = (R.num > 0) && (R.den > 0);
|
||||
|
||||
// Unit
|
||||
template<ratio R, typename U>
|
||||
requires UnitRatio<R>
|
||||
template<Magnitude auto M, typename U>
|
||||
struct scaled_unit;
|
||||
|
||||
// TODO: Remove when P1985 accepted
|
||||
namespace detail {
|
||||
|
||||
template<ratio R, typename U>
|
||||
void to_base_scaled_unit(const volatile scaled_unit<R, U>*);
|
||||
template<Magnitude auto M, typename U>
|
||||
void to_base_scaled_unit(const volatile scaled_unit<M, U>*);
|
||||
|
||||
} // namespace detail
|
||||
|
||||
|
||||
@@ -55,22 +55,22 @@ struct common_quantity_reference_impl<reference<D, U>, reference<D, U>> {
|
||||
|
||||
template<typename D, typename U1, typename U2>
|
||||
struct common_quantity_reference_impl<reference<D, U1>, reference<D, U2>> {
|
||||
using type = reference<D, downcast_unit<D, common_ratio(U1::ratio, U2::ratio)>>;
|
||||
using type = reference<D, downcast_unit<D, common_magnitude(U1::mag, U2::mag)>>;
|
||||
};
|
||||
|
||||
template<typename D1, typename U1, typename D2, typename U2>
|
||||
requires(same_unit_reference<dimension_unit<D1>, dimension_unit<D2>>::value)
|
||||
struct common_quantity_reference_impl<reference<D1, U1>, reference<D2, U2>> {
|
||||
using type = reference<D1, downcast_unit<D1, common_ratio(U1::ratio, U2::ratio)>>;
|
||||
using type = reference<D1, downcast_unit<D1, common_magnitude(U1::mag, U2::mag)>>;
|
||||
};
|
||||
|
||||
template<typename D1, typename U1, typename D2, typename U2>
|
||||
struct common_quantity_reference_impl<reference<D1, U1>, reference<D2, U2>> {
|
||||
using dimension = conditional<is_specialization_of<D1, unknown_dimension>, D2, D1>;
|
||||
static constexpr ratio r1 = D1::base_units_ratio * U1::ratio;
|
||||
static constexpr ratio r2 = D2::base_units_ratio * U2::ratio;
|
||||
static constexpr ratio cr = common_ratio(r1, r2);
|
||||
using unit = downcast_unit<dimension, cr / dimension::base_units_ratio>;
|
||||
static constexpr Magnitude auto m1 = D1::base_units_ratio * U1::mag;
|
||||
static constexpr Magnitude auto m2 = D2::base_units_ratio * U2::mag;
|
||||
static constexpr Magnitude auto cm = common_magnitude(m1, m2);
|
||||
using unit = downcast_unit<dimension, cm / dimension::base_units_ratio>;
|
||||
using type = reference<dimension, unit>;
|
||||
};
|
||||
|
||||
|
||||
@@ -43,12 +43,6 @@ constexpr Magnitude auto derived_mag(exponent_list<Es...>)
|
||||
pow<ratio{Es::num, Es::den}>(Us::mag / dimension_unit<typename Es::dimension>::mag));
|
||||
}
|
||||
|
||||
template<Unit... Us, typename... Es>
|
||||
constexpr ratio derived_ratio(exponent_list<Es...> es)
|
||||
{
|
||||
return as_ratio(derived_mag<Us...>(es));
|
||||
}
|
||||
|
||||
template<DerivedDimension D, Unit... Us>
|
||||
using derived_unit = scaled_unit<derived_mag<Us...>(typename D::recipe()), typename D::coherent_unit::reference>;
|
||||
|
||||
|
||||
Reference in new issue
Block a user