xo-unit: quantity refactoring
This commit is contained in:
parent
b0ce5eaee9
commit
b2d7bf93d2
3 changed files with 52 additions and 46 deletions
|
|
@ -65,11 +65,7 @@ Installing from source
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
Install scripts for `xo-unit`, `xo-ratio` and `xo-flatstring` depend on shared cmake macros
|
Install scripts for `xo-unit`, `xo-ratio` and `xo-flatstring` depend on shared cmake macros
|
||||||
and a bootstrap script `xo-cmake-config`.
|
and a bootstrap script `xo-cmake-config` fro `xo-cmake`.
|
||||||
|
|
||||||
* `xo-cmake`_ source
|
|
||||||
|
|
||||||
.. _xo-cmake: https://github.com/rconybea/xo-cmake
|
|
||||||
|
|
||||||
Preamble:
|
Preamble:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -264,8 +264,23 @@ namespace xo {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// operator*=
|
/** multiply @p y in-place. y must be dimensionless **/
|
||||||
// operator/=
|
template <typename Dimensionless>
|
||||||
|
requires std::is_arithmetic_v<Dimensionless>
|
||||||
|
constexpr
|
||||||
|
quantity & operator*=(Dimensionless y) {
|
||||||
|
this->scale_ *= y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** divide @p y in-place. y must be dimensionless **/
|
||||||
|
template <typename Dimensionless>
|
||||||
|
requires std::is_arithmetic_v<Dimensionless>
|
||||||
|
constexpr
|
||||||
|
quantity & operator/=(Dimensionless y) {
|
||||||
|
this->scale_ /= y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
///@}
|
///@}
|
||||||
|
|
||||||
|
|
@ -634,6 +649,40 @@ namespace xo {
|
||||||
return detail::quantity_util::subtract(x, y);
|
return detail::quantity_util::subtract(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** subtract an arithmetic value from a dimensionless quantity **/
|
||||||
|
template <typename Quantity,
|
||||||
|
typename Dimensionless>
|
||||||
|
requires (quantity_concept<Quantity>
|
||||||
|
&& Quantity::is_dimensionless()
|
||||||
|
&& std::is_arithmetic_v<Dimensionless>)
|
||||||
|
constexpr auto
|
||||||
|
operator- (const Quantity & x, Dimensionless y)
|
||||||
|
{
|
||||||
|
using repr_type = std::common_type_t<typename Quantity::repr_type, Dimensionless>;
|
||||||
|
|
||||||
|
auto xp = static_cast<repr_type>(x.scale());
|
||||||
|
auto yp = static_cast<repr_type>(y);
|
||||||
|
|
||||||
|
return xp - yp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** subtract a dimensionless quantity from an arithmetic value **/
|
||||||
|
template <typename Dimensionless,
|
||||||
|
typename Quantity>
|
||||||
|
requires (std::is_arithmetic_v<Dimensionless>
|
||||||
|
&& quantity_concept<Quantity>
|
||||||
|
&& Quantity::is_dimensionless())
|
||||||
|
constexpr auto
|
||||||
|
operator- (Dimensionless x, const Quantity & y)
|
||||||
|
{
|
||||||
|
using repr_type = std::common_type_t<Dimensionless, typename Quantity::repr_type>;
|
||||||
|
|
||||||
|
auto xp = static_cast<repr_type>(x);
|
||||||
|
auto yp = static_cast<repr_type>(y.scale());
|
||||||
|
|
||||||
|
return xp - yp;
|
||||||
|
}
|
||||||
|
|
||||||
///@}
|
///@}
|
||||||
|
|
||||||
namespace qty {
|
namespace qty {
|
||||||
|
|
|
||||||
|
|
@ -9,45 +9,6 @@
|
||||||
|
|
||||||
namespace xo {
|
namespace xo {
|
||||||
namespace qty {
|
namespace qty {
|
||||||
/** @defgroup quantity-dimensionless-operators **/
|
|
||||||
///@{
|
|
||||||
|
|
||||||
/** subtract an arithmetic value from a dimensionless quantity **/
|
|
||||||
template <typename Quantity,
|
|
||||||
typename Dimensionless>
|
|
||||||
requires (quantity_concept<Quantity>
|
|
||||||
&& Quantity::is_dimensionless()
|
|
||||||
&& std::is_arithmetic_v<Dimensionless>)
|
|
||||||
constexpr auto
|
|
||||||
operator- (const Quantity & x, Dimensionless y)
|
|
||||||
{
|
|
||||||
using repr_type = std::common_type_t<typename Quantity::repr_type, Dimensionless>;
|
|
||||||
|
|
||||||
auto xp = static_cast<repr_type>(x.scale());
|
|
||||||
auto yp = static_cast<repr_type>(y);
|
|
||||||
|
|
||||||
return xp - yp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** subtract a dimensionless quantity from an arithmetic value **/
|
|
||||||
template <typename Dimensionless,
|
|
||||||
typename Quantity>
|
|
||||||
requires (std::is_arithmetic_v<Dimensionless>
|
|
||||||
&& quantity_concept<Quantity>
|
|
||||||
&& Quantity::is_dimensionless())
|
|
||||||
constexpr auto
|
|
||||||
operator- (Dimensionless x, const Quantity & y)
|
|
||||||
{
|
|
||||||
using repr_type = std::common_type_t<Dimensionless, typename Quantity::repr_type>;
|
|
||||||
|
|
||||||
auto xp = static_cast<repr_type>(x);
|
|
||||||
auto yp = static_cast<repr_type>(y.scale());
|
|
||||||
|
|
||||||
return xp - yp;
|
|
||||||
}
|
|
||||||
|
|
||||||
///@}
|
|
||||||
|
|
||||||
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
|
||||||
**/
|
**/
|
||||||
template <typename Quantity, typename Quantity2>
|
template <typename Quantity, typename Quantity2>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue