xo-unit: promote to common repr in mixed-type operator* w/ quantity
Avoid truncating when, for example, multiplying quantity<Unit, int> * double. Add targeted unit test for this
This commit is contained in:
parent
3e86011f51
commit
df6cfbb25c
2 changed files with 22 additions and 1 deletions
|
|
@ -106,7 +106,9 @@ namespace xo {
|
|||
template <typename Dimensionless>
|
||||
requires std::is_arithmetic_v<Dimensionless>
|
||||
constexpr auto scale_by(Dimensionless x) const {
|
||||
return quantity(x * this->scale_);
|
||||
using r_repr_type = std::common_type_t<repr_type, Dimensionless>;
|
||||
|
||||
return quantity<s_unit, r_repr_type>(x * this->scale_);
|
||||
}
|
||||
|
||||
// divide_by
|
||||
|
|
|
|||
|
|
@ -421,6 +421,25 @@ namespace xo {
|
|||
|
||||
} /*TEST_CASE(quantity.mult2)*/
|
||||
|
||||
TEST_CASE("quantity.mult3", "[quantity.mult]") {
|
||||
constexpr auto ng = qty::nanogram;
|
||||
|
||||
constexpr auto l_qty = 7.55 * ng;
|
||||
constexpr auto r_qty = ng * 7.55;
|
||||
|
||||
static_assert(l_qty.unit().n_bpu() == 1);
|
||||
static_assert(l_qty.unit().bpu_v_[0].native_dim() == dim::mass);
|
||||
static_assert(l_qty.unit().bpu_v_[0].power() == xo::ratio::ratio(1,1));
|
||||
static_assert(l_qty.unit().bpu_v_[0].scalefactor() == xo::ratio::ratio(1,1000000000));
|
||||
static_assert(l_qty.scale() == 7.55);
|
||||
|
||||
static_assert(r_qty.unit().n_bpu() == 1);
|
||||
static_assert(r_qty.unit().bpu_v_[0].native_dim() == dim::mass);
|
||||
static_assert(r_qty.unit().bpu_v_[0].power() == xo::ratio::ratio(1,1));
|
||||
static_assert(r_qty.unit().bpu_v_[0].scalefactor() == xo::ratio::ratio(1,1000000000));
|
||||
static_assert(r_qty.scale() == 7.55);
|
||||
}
|
||||
|
||||
TEST_CASE("quantity.div2", "[quantity.div]") {
|
||||
constexpr auto ms = qty::milliseconds(1.0);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue