From 8ec337c58930f0e1909341e6320fab2e7b03f5eb Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 24 Apr 2024 11:31:10 -0400 Subject: [PATCH] xo-unit: + Quantity.rescale() --- include/xo/unit/Quantity.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/xo/unit/Quantity.hpp b/include/xo/unit/Quantity.hpp index 0a4fc8b6..a70fcdee 100644 --- a/include/xo/unit/Quantity.hpp +++ b/include/xo/unit/Quantity.hpp @@ -45,6 +45,21 @@ namespace xo { constexpr Quantity unit_qty() const { return Quantity(1, unit_); } constexpr Quantity reciprocal() const { return Quantity(1.0 / scale_, unit_.reciprocal()); } + constexpr + auto rescale(const natural_unit & unit2) const { + /* conversion factor from .unit -> unit2*/ + auto rr = detail::nu_ratio(this->unit_, unit2); + + if (rr.natural_unit_.is_dimensionless()) { + repr_type r_scale = (::sqrt(rr.outer_scale_sq_) + * rr.outer_scale_exact_.template to() + * this->scale_); + return Quantity(r_scale, unit2); + } else { + return Quantity(std::numeric_limits::quiet_NaN(), unit2); + } + } + template static constexpr auto multiply(const Quantity & x, const Quantity2 & y) {