mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-03 12:24:26 +02:00
feat: runtime conversion factors between units support added
This commit is contained in:
@@ -39,8 +39,6 @@ inline constexpr struct great_british_pound : named_unit<"GBP", kind_of<currency
|
||||
inline constexpr struct japanese_jen : named_unit<"JPY", kind_of<currency>> {} japanese_jen;
|
||||
// clang-format on
|
||||
|
||||
static_assert(!std::equality_comparable_with<quantity<euro, int>, quantity<us_dollar, int>>);
|
||||
|
||||
#if 0
|
||||
|
||||
// if you have only a few currencies to handle
|
||||
@@ -54,6 +52,11 @@ template<Unit auto From, Unit auto To>
|
||||
|
||||
#else
|
||||
|
||||
template<AssociatedUnit auto From, AssociatedUnit auto To>
|
||||
requires(get_quantity_spec(From) == currency && get_quantity_spec(To) == currency)
|
||||
inline constexpr is_unit_convertible_result mp_units::is_unit_convertible<From, To> =
|
||||
is_unit_convertible_result::non_integral_factor;
|
||||
|
||||
[[nodiscard]] std::string_view to_string_view(Unit auto u) { return u.symbol.ascii().c_str(); }
|
||||
|
||||
template<Unit auto From, Unit auto To>
|
||||
@@ -69,23 +72,21 @@ template<Unit auto From, Unit auto To>
|
||||
|
||||
#endif
|
||||
|
||||
template<ReferenceOf<currency> auto To, ReferenceOf<currency> auto From, typename Rep>
|
||||
quantity<To, Rep> exchange_to(quantity<From, Rep> q)
|
||||
template<typename Rep, AssociatedUnit From, AssociatedUnit To>
|
||||
[[nodiscard]] Rep scale_quantity_number(Rep v, From from, To to)
|
||||
requires(get_quantity_spec(from) == currency && get_quantity_spec(to) == currency)
|
||||
{
|
||||
return static_cast<Rep>(exchange_rate<q.unit, get_unit(To)>() * q.number()) * To;
|
||||
return static_cast<Rep>(exchange_rate<from, to>() * v);
|
||||
}
|
||||
|
||||
template<ReferenceOf<currency> auto To, ReferenceOf<currency> auto From, auto PO, typename Rep>
|
||||
quantity_point<To, PO, Rep> exchange_to(quantity_point<From, PO, Rep> q)
|
||||
{
|
||||
return static_cast<Rep>(exchange_rate<q.unit, get_unit(To)>() * q.absolute().number()) * To;
|
||||
}
|
||||
static_assert(!std::equality_comparable_with<quantity<euro>, quantity<us_dollar>>);
|
||||
|
||||
int main()
|
||||
{
|
||||
auto price_usd = quantity_point{100 * us_dollar};
|
||||
auto price_euro = quantity_point{exchange_to<euro>(price_usd)};
|
||||
auto price_usd = quantity_point{100. * us_dollar};
|
||||
auto price_euro = quantity_point{price_usd[euro]};
|
||||
|
||||
std::cout << price_usd.absolute() << " -> " << price_euro.absolute() << "\n";
|
||||
// std::cout << price_usd.absolute() + price_euro.absolute() << "\n"; // does not compile
|
||||
}
|
||||
// std::cout << price_euro.absolute() + price_usd.absolute() << "\n"; // does not compile
|
||||
}
|
||||
|
@@ -35,6 +35,32 @@ class quantity;
|
||||
|
||||
namespace detail {
|
||||
|
||||
// The default implementation for the number scaling customization point
|
||||
template<typename Rep, Unit From, Unit To>
|
||||
[[nodiscard]] constexpr auto scale_quantity_number(Rep v, From from, To to)
|
||||
requires(have_same_canonical_reference_unit(from, to))
|
||||
{
|
||||
using multiplier_type = decltype([] {
|
||||
// widen the type to prevent overflows
|
||||
using wider_type = decltype(Rep{} * std::intmax_t{});
|
||||
// check if `wider_type` supports scaling operations
|
||||
if constexpr (requires(wider_type v) { v* v / v; })
|
||||
// if the `wider_type` can handle scaling operations then use it to improve accuracy
|
||||
return wider_type{};
|
||||
else
|
||||
// needed for example for linear algebra where `op/` on matrix types is not available
|
||||
return std::intmax_t{};
|
||||
}());
|
||||
|
||||
constexpr Magnitude auto c_mag = detail::get_canonical_unit(from).mag / detail::get_canonical_unit(to).mag;
|
||||
constexpr Magnitude auto num = numerator(c_mag);
|
||||
constexpr Magnitude auto den = denominator(c_mag);
|
||||
constexpr Magnitude auto irr = c_mag * (den / num);
|
||||
|
||||
constexpr auto val = [](Magnitude auto m) { return get_value<multiplier_type>(m); };
|
||||
return v * val(num) / val(den) * val(irr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Explicit cast of entire quantity
|
||||
*
|
||||
@@ -45,7 +71,7 @@ namespace detail {
|
||||
template<Quantity To, auto R, typename Rep>
|
||||
requires(castable(get_quantity_spec(R), To::quantity_spec)) &&
|
||||
((get_unit(R) == To::unit && std::constructible_from<typename To::rep, Rep>) ||
|
||||
(get_unit(R) != To::unit)) // && scalable_with_<typename To::rep>))
|
||||
(get_unit(R) != To::unit && convertible(get_unit(R), To::unit))) // && scalable_with_<typename To::rep>))
|
||||
// TODO how to constrain the second part here?
|
||||
[[nodiscard]] constexpr Quantity auto sudo_cast(const quantity<R, Rep>& q)
|
||||
{
|
||||
@@ -66,26 +92,11 @@ template<Quantity To, auto R, typename Rep>
|
||||
else
|
||||
return Rep{};
|
||||
}());
|
||||
using multiplier_type = decltype([] {
|
||||
// widen the type to prevent overflows
|
||||
using wider_type = decltype(rep_type{} * std::intmax_t{});
|
||||
// check if `wider_type` supports scaling operations
|
||||
if constexpr (requires(wider_type v) { v* v / v; })
|
||||
// if the `wider_type` can handle scaling operations then use it to improve accuracy
|
||||
return wider_type{};
|
||||
else
|
||||
// needed for example for linear algebra where `op/` on matrix types is not available
|
||||
return std::intmax_t{};
|
||||
}());
|
||||
|
||||
constexpr Magnitude auto c_mag =
|
||||
detail::get_canonical_unit(get_unit(R)).mag / detail::get_canonical_unit(To::unit).mag;
|
||||
constexpr Magnitude auto num = numerator(c_mag);
|
||||
constexpr Magnitude auto den = denominator(c_mag);
|
||||
constexpr Magnitude auto irr = c_mag * (den / num);
|
||||
|
||||
constexpr auto val = [](Magnitude auto m) { return get_value<multiplier_type>(m); };
|
||||
return static_cast<TYPENAME To::rep>(static_cast<rep_type>(q.number()) * val(num) / val(den) * val(irr)) *
|
||||
// `scale_quantity_number` is a customization point
|
||||
// Will be found only via ADL (if provided) for user-defined units
|
||||
return static_cast<TYPENAME To::rep>(
|
||||
scale_quantity_number(static_cast<rep_type>(q.number()), get_unit(R), To::unit)) *
|
||||
To::reference;
|
||||
}
|
||||
}
|
||||
|
@@ -50,18 +50,13 @@ template<typename T, typename Arg>
|
||||
concept RepSafeConstructibleFrom = // exposition only
|
||||
std::constructible_from<T, Arg> && (treat_as_floating_point<T> || !treat_as_floating_point<Arg>);
|
||||
|
||||
// UFrom ratio is an exact multiple of UTo
|
||||
template<auto UFrom, auto UTo>
|
||||
concept Harmonic = // exposition only
|
||||
Unit<decltype(UFrom)> && Unit<decltype(UTo)> &&
|
||||
is_integral(get_canonical_unit(UFrom).mag / get_canonical_unit(UTo).mag);
|
||||
|
||||
template<typename QFrom, typename QTo>
|
||||
concept QuantityConvertibleTo = // exposition only
|
||||
Quantity<QFrom> && Quantity<QTo> && implicitly_convertible(QFrom::quantity_spec, QTo::quantity_spec) &&
|
||||
convertible(QFrom::unit, QTo::unit) && requires(QFrom q) { detail::sudo_cast<QTo>(q); } &&
|
||||
(treat_as_floating_point<typename QTo::rep> ||
|
||||
(!treat_as_floating_point<typename QFrom::rep> && Harmonic<QFrom::unit, QTo::unit>));
|
||||
(!treat_as_floating_point<typename QFrom::rep> &&
|
||||
is_unit_convertible<QFrom::unit, QTo::unit> == is_unit_convertible_result::integral_factor));
|
||||
|
||||
template<quantity_character Ch, typename Func, typename T, typename U>
|
||||
concept InvokeResultOf = std::regular_invocable<Func, T, U> && RepresentationOf<std::invoke_result_t<Func, T, U>, Ch>;
|
||||
|
@@ -594,11 +594,21 @@ inline constexpr struct percent : named_unit<"%", mag<ratio{1, 100}> * one> {} p
|
||||
inline constexpr struct per_mille : named_unit<basic_symbol_text{"‰", "%o"}, mag<ratio(1, 1000)> * one> {} per_mille;
|
||||
// clang-format on
|
||||
|
||||
// is_unit_convertible customization point
|
||||
enum class is_unit_convertible_result { no, integral_factor, non_integral_factor };
|
||||
|
||||
// convertible_to
|
||||
template<Unit auto From, Unit auto To>
|
||||
inline constexpr is_unit_convertible_result is_unit_convertible =
|
||||
detail::have_same_canonical_reference_unit(From, To)
|
||||
? (is_integral(detail::get_canonical_unit(From).mag / detail::get_canonical_unit(To).mag)
|
||||
? is_unit_convertible_result::integral_factor
|
||||
: is_unit_convertible_result::non_integral_factor)
|
||||
: is_unit_convertible_result::no;
|
||||
|
||||
// convertible
|
||||
[[nodiscard]] consteval bool convertible(Unit auto from, Unit auto to)
|
||||
{
|
||||
return detail::have_same_canonical_reference_unit(from, to);
|
||||
return is_unit_convertible<from, to> != is_unit_convertible_result::no;
|
||||
}
|
||||
|
||||
// Common unit
|
||||
|
Reference in New Issue
Block a user