From 492e5bdd3878d0d6e65c9d4eba8dc305db749272 Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Thu, 7 Mar 2024 17:33:03 -0500 Subject: [PATCH] Perform QuantityPoint subtraction in target rep (#223) If we don't do this, then converting from unsigned to signed of the same size will perform the subtraction in the unsigned type. Included a unit test that failed before the change, but passes with it. Fixes #222. --- au/quantity_point.hh | 3 ++- au/quantity_point_test.cc | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/au/quantity_point.hh b/au/quantity_point.hh index 65b752ad..816d7b50 100644 --- a/au/quantity_point.hh +++ b/au/quantity_point.hh @@ -132,7 +132,8 @@ class QuantityPoint { typename NewUnit, typename = std::enable_if_t::value>> constexpr NewRep in(NewUnit u) const { - return (x_ - OriginDisplacement::value()).template in(u); + return (rep_cast(x_) - rep_cast(OriginDisplacement::value())) + .template in(u); } template ::value>> diff --git a/au/quantity_point_test.cc b/au/quantity_point_test.cc index d5a63c2e..cc7457db 100644 --- a/au/quantity_point_test.cc +++ b/au/quantity_point_test.cc @@ -98,6 +98,11 @@ TEST(QuantityPoint, CanGetValueInDifferentUnits) { EXPECT_THAT(p.in(centi(meters_pt)), SameTypeAndValue(300)); } +TEST(QuantityPoint, IntermediateTypeIsSignedIfExplicitRepIsSigned) { + EXPECT_THAT(milli(kelvins_pt)(0u).coerce_as(celsius_pt), + SameTypeAndValue(celsius_pt(-273))); +} + TEST(QuantityPoint, SupportsDirectAccessWithSameUnit) { auto p = celsius_pt(3); ++(p.data_in(Celsius{}));