Skip to content

Commit

Permalink
Perform QuantityPoint subtraction in target rep (#223)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chiphogg authored Mar 7, 2024
1 parent 8153761 commit 492e5bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion au/quantity_point.hh
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ class QuantityPoint {
typename NewUnit,
typename = std::enable_if_t<IsUnit<NewUnit>::value>>
constexpr NewRep in(NewUnit u) const {
return (x_ - OriginDisplacement<Unit, NewUnit>::value()).template in<NewRep>(u);
return (rep_cast<NewRep>(x_) - rep_cast<NewRep>(OriginDisplacement<Unit, NewUnit>::value()))
.template in<NewRep>(u);
}

template <typename NewUnit, typename = std::enable_if_t<IsUnit<NewUnit>::value>>
Expand Down
5 changes: 5 additions & 0 deletions au/quantity_point_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(celsius_pt),
SameTypeAndValue(celsius_pt(-273)));
}

TEST(QuantityPoint, SupportsDirectAccessWithSameUnit) {
auto p = celsius_pt(3);
++(p.data_in(Celsius{}));
Expand Down

0 comments on commit 492e5bd

Please sign in to comment.