Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mpusz/mp-units
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed May 16, 2024
2 parents 5dc21fd + 3ccdcdd commit 15a6d02
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/core/include/mp-units/framework/quantity_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ namespace mp_units {
* @tparam ToQS a quantity specification to use for a target quantity
*/
template<QuantitySpec auto ToQS, typename Q>
requires Quantity<std::remove_cvref_t<Q>> && (castable(Q::quantity_spec, ToQS))
requires Quantity<std::remove_cvref_t<Q>> && (castable(std::remove_reference_t<Q>::quantity_spec, ToQS))
[[nodiscard]] constexpr Quantity auto quantity_cast(Q&& q)
{
return quantity{std::forward<Q>(q).numerical_value_is_an_implementation_detail_, make_reference(ToQS, Q::unit)};
return quantity{std::forward<Q>(q).numerical_value_is_an_implementation_detail_,
make_reference(ToQS, std::remove_reference_t<Q>::unit)};
}

/**
Expand All @@ -77,11 +78,11 @@ template<QuantitySpec auto ToQS, typename Q>
* @tparam ToQS a quantity specification to use for a target quantity point
*/
template<QuantitySpec auto ToQS, typename QP>
requires QuantityPoint<std::remove_cvref_t<QP>> && (castable(QP::quantity_spec, ToQS))
requires QuantityPoint<std::remove_cvref_t<QP>> && (castable(std::remove_reference_t<QP>::quantity_spec, ToQS))
[[nodiscard]] constexpr QuantityPoint auto quantity_cast(QP&& qp)
{
return QP{quantity_cast<ToQS>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_),
qp.point_origin};
std::remove_reference_t<QP>::point_origin};
}

} // namespace mp_units
12 changes: 7 additions & 5 deletions src/core/include/mp-units/framework/value_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ template<Unit auto ToU, typename QP>
[[nodiscard]] constexpr QuantityPoint auto value_cast(QP&& qp)
{
return quantity_point{value_cast<ToU>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_),
qp.point_origin};
std::remove_reference_t<QP>::point_origin};
}

/**
Expand All @@ -124,10 +124,12 @@ template<Representation ToRep, typename QP>
requires QuantityPoint<std::remove_cvref_t<QP>> &&
RepresentationOf<ToRep, std::remove_reference_t<QP>::quantity_spec.character> &&
std::constructible_from<ToRep, typename std::remove_reference_t<QP>::rep>
[[nodiscard]] constexpr quantity_point<std::remove_reference_t<QP>::reference, QP::point_origin, ToRep> value_cast(
QP&& qp)
[[nodiscard]] constexpr quantity_point<std::remove_reference_t<QP>::reference,
std::remove_reference_t<QP>::point_origin, ToRep>
value_cast(QP&& qp)
{
return {value_cast<ToRep>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_), qp.point_origin};
return {value_cast<ToRep>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_),
std::remove_reference_t<QP>::point_origin};
}

/**
Expand All @@ -147,7 +149,7 @@ template<Unit auto ToU, Representation ToRep, typename QP>
[[nodiscard]] constexpr QuantityPoint auto value_cast(QP&& qp)
{
return quantity_point{value_cast<ToU, ToRep>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_),
qp.point_origin};
std::remove_reference_t<QP>::point_origin};
}

} // namespace mp_units
59 changes: 50 additions & 9 deletions src/core/include/mp-units/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <mp-units/bits/module_macros.h>
#include <mp-units/framework/customization_points.h>
#include <mp-units/framework/quantity.h>
#include <mp-units/framework/quantity_point.h>
#include <mp-units/framework/unit.h>
#include <mp-units/framework/value_cast.h>

Expand Down Expand Up @@ -130,10 +131,10 @@ template<auto R, typename Rep>
}

/**
* @brief Determines if a number is finite.
* @brief Determines if a quantity is finite.
*
* @param a: Number to analyze.
* @return bool: Whether the number is finite or not.
* @param a: Quantity to analyze.
* @return bool: Whether the quantity is finite or not.
*/
template<auto R, typename Rep>
requires requires(Rep v) { isfinite(v); } || requires(Rep v) { std::isfinite(v); }
Expand All @@ -144,10 +145,23 @@ template<auto R, typename Rep>
}

/**
* @brief Determines if a number is infinite.
* @brief Determines if a quantity point is finite.
*
* @param a: Number to analyze.
* @return bool: Whether the number is infinite or not.
* @param a: Quantity point to analyze.
* @return bool: Whether the quantity point is finite or not.
*/
template<auto R, auto PO, typename Rep>
requires requires(quantity<R, Rep> q) { isfinite(q); }
[[nodiscard]] constexpr bool isfinite(const quantity_point<R, PO, Rep>& a) noexcept
{
return isfinite(a.quantity_ref_from(a.point_origin));
}

/**
* @brief Determines if a quantity is infinite.
*
* @param a: Quantity to analyze.
* @return bool: Whether the quantity is infinite or not.
*/
template<auto R, typename Rep>
requires requires(Rep v) { isinf(v); } || requires(Rep v) { std::isinf(v); }
Expand All @@ -157,12 +171,25 @@ template<auto R, typename Rep>
return isinf(a.numerical_value_ref_in(a.unit));
}

/**
* @brief Determines if a quantity point is infinite.
*
* @param a: Quantity point to analyze.
* @return bool: Whether the quantity point is infinite or not.
*/
template<auto R, auto PO, typename Rep>
requires requires(quantity<R, Rep> q) { isinf(q); }
[[nodiscard]] constexpr bool isinf(const quantity_point<R, PO, Rep>& a) noexcept
{
return isinf(a.quantity_ref_from(a.point_origin));
}


/**
* @brief Determines if a number is a nan.
* @brief Determines if a quantity is a nan.
*
* @param a: Number to analyze.
* @return bool: Whether the number is a NaN or not.
* @param a: Quantity to analyze.
* @return bool: Whether the quantity is a NaN or not.
*/
template<auto R, typename Rep>
requires requires(Rep v) { isnan(v); } || requires(Rep v) { std::isnan(v); }
Expand All @@ -172,6 +199,20 @@ template<auto R, typename Rep>
return isnan(a.numerical_value_ref_in(a.unit));
}


/**
* @brief Determines if a quantity point is a nan.
*
* @param a: Quantity point to analyze.
* @return bool: Whether the quantity point is a NaN or not.
*/
template<auto R, auto PO, typename Rep>
requires requires(quantity<R, Rep> q) { isnan(q); }
[[nodiscard]] constexpr bool isnan(const quantity_point<R, PO, Rep>& a) noexcept
{
return isnan(a.quantity_ref_from(a.point_origin));
}

/**
* @brief Computes the fma of 3 quantities
*
Expand Down
26 changes: 26 additions & 0 deletions src/systems/include/mp-units/systems/si/unit_symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,32 @@ inline constexpr auto YF = yotta<farad>;
inline constexpr auto RF = ronna<farad>;
inline constexpr auto QF = quetta<farad>;

inline constexpr auto qohm = quecto<si::ohm>;
inline constexpr auto rohm = ronto<si::ohm>;
inline constexpr auto yohm = yocto<si::ohm>;
inline constexpr auto zohm = zepto<si::ohm>;
inline constexpr auto aohm = atto<si::ohm>;
inline constexpr auto fohm = femto<si::ohm>;
inline constexpr auto pohm = pico<si::ohm>;
inline constexpr auto nohm = nano<si::ohm>;
inline constexpr auto uohm = micro<si::ohm>;
inline constexpr auto mohm = milli<si::ohm>;
inline constexpr auto cohm = centi<si::ohm>;
inline constexpr auto dohm = deci<si::ohm>;
using si::ohm;
inline constexpr auto daohm = deca<si::ohm>;
inline constexpr auto hohm = hecto<si::ohm>;
inline constexpr auto kohm = kilo<si::ohm>;
inline constexpr auto Mohm = mega<si::ohm>;
inline constexpr auto Gohm = giga<si::ohm>;
inline constexpr auto Tohm = tera<si::ohm>;
inline constexpr auto Pohm = peta<si::ohm>;
inline constexpr auto Eohm = exa<si::ohm>;
inline constexpr auto Zohm = zetta<si::ohm>;
inline constexpr auto Yohm = yotta<si::ohm>;
inline constexpr auto Rohm = ronna<si::ohm>;
inline constexpr auto Qohm = quetta<si::ohm>;

inline constexpr auto qS = quecto<siemens>;
inline constexpr auto rS = ronto<siemens>;
inline constexpr auto yS = yocto<siemens>;
Expand Down
15 changes: 15 additions & 0 deletions test/static/quantity_point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1677,4 +1677,19 @@ static_assert(invalid_addition(5 * isq::activity[Bq], 10 / (2 * isq::time[s]), q
static_assert(invalid_subtraction(quantity_point{5 * isq::activity[Bq]}, 10 / (2 * isq::time[s]),
5 * isq::frequency[Hz]));

// value_cast

static_assert(value_cast<m>(quantity_point{2 * km}).quantity_from_zero().numerical_value_in(m) == 2000);
static_assert(value_cast<km>(quantity_point{2000 * m}).quantity_from_zero().numerical_value_in(km) == 2);
static_assert(value_cast<int>(quantity_point{1.23 * m}).quantity_from_zero().numerical_value_in(m) == 1);
static_assert(
value_cast<km / h>(quantity_point{2000.0 * m / (3600.0 * s)}).quantity_from_zero().numerical_value_in(km / h) == 2);
// lvalue references in value_cast
namespace lvalue_tests {
constexpr quantity_point lvalue_qp{2 * km};
static_assert(value_cast<m>(lvalue_qp).quantity_from_zero().numerical_value_in(m) == 2000);
static_assert(value_cast<float>(lvalue_qp).quantity_from_zero().numerical_value_in(km) == 2.f);
static_assert(value_cast<m, float>(lvalue_qp).quantity_from_zero().numerical_value_in(m) == 2000.f);
} // namespace lvalue_tests

} // namespace
6 changes: 5 additions & 1 deletion test/static/quantity_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,11 @@ static_assert(is_of_type<quantity_cast<isq::distance>(1 * m), quantity<isq::dist
static_assert(is_of_type<quantity_cast<isq::distance>(isq::length(1 * m)), quantity<isq::distance[m], int>>);
static_assert(is_of_type<quantity_cast<kind_of<isq::length>>(isq::length(1 * m)), quantity<si::metre, int>>);
static_assert(is_of_type<quantity_cast<kind_of<isq::length>>(isq::distance(1 * m)), quantity<si::metre, int>>);

// lvalue references in quantity_cast
namespace lvalue_tests {
constexpr quantity<m, int> lvalue_q = 1 * m;
static_assert(is_of_type<quantity_cast<isq::distance>(lvalue_q), quantity<isq::distance[m], int>>);
} // namespace lvalue_tests

// QuantityOf
static_assert(QuantityOf<quantity<isq::length[m]>, isq::length>);
Expand Down

0 comments on commit 15a6d02

Please sign in to comment.