diff --git a/include/units/core.h b/include/units/core.h index c1cdaed0..daf2f06d 100644 --- a/include/units/core.h +++ b/include/units/core.h @@ -3737,7 +3737,7 @@ namespace units constexpr auto sqrt(const UnitType& value) noexcept -> unit::conversion_factor>>, detail::floating_point_promotion_t::underlying_type>, linear_scale> { - return decltype(units::sqrt(value))(sqrt(value.value())); + return decltype(units::sqrt(value))(sqrt(value.raw())); } /** @@ -3771,7 +3771,7 @@ namespace units template, int> = 0> detail::floating_point_promotion_t ceil(const UnitType x) noexcept { - return detail::floating_point_promotion_t(std::ceil(x.value())); + return detail::floating_point_promotion_t(std::ceil(x.raw())); } /** @@ -3784,7 +3784,7 @@ namespace units template, int> = 0> detail::floating_point_promotion_t floor(const UnitType x) noexcept { - return detail::floating_point_promotion_t(std::floor(x.value())); + return detail::floating_point_promotion_t(std::floor(x.raw())); } /** @@ -3813,7 +3813,7 @@ namespace units template, int> = 0> detail::floating_point_promotion_t trunc(const UnitType x) noexcept { - return detail::floating_point_promotion_t(std::trunc(x.value())); + return detail::floating_point_promotion_t(std::trunc(x.raw())); } /** @@ -3827,7 +3827,7 @@ namespace units template, int> = 0> detail::floating_point_promotion_t round(const UnitType x) noexcept { - return detail::floating_point_promotion_t(std::round(x.value())); + return detail::floating_point_promotion_t(std::round(x.raw())); } //---------------------------------- @@ -3846,14 +3846,14 @@ namespace units template && traits::is_unit_v, int> = 0> detail::floating_point_promotion_t copysign(const UnitTypeLhs x, const UnitTypeRhs y) noexcept { - return detail::floating_point_promotion_t(std::copysign(x.value(), y.value())); // no need for conversion to get the correct sign. + return detail::floating_point_promotion_t(std::copysign(x.raw(), y.raw())); // no need for conversion to get the correct sign. } /// Overload to copy the sign from a raw double template && traits::is_unit_v, int> = 0> detail::floating_point_promotion_t copysign(const UnitTypeLhs x, const T& y) noexcept { - return detail::floating_point_promotion_t(std::copysign(x.value(), y)); + return detail::floating_point_promotion_t(std::copysign(x.raw(), y)); } //---------------------------------- @@ -3872,7 +3872,7 @@ namespace units detail::floating_point_promotion_t> fdim(const UnitTypeLhs x, const UnitTypeRhs y) noexcept { using CommonUnit = decltype(units::fdim(x, y)); - return CommonUnit(std::fdim(CommonUnit(x).value(), CommonUnit(y).value())); + return CommonUnit(std::fdim(CommonUnit(x).raw(), CommonUnit(y).raw())); } /** @@ -3887,7 +3887,7 @@ namespace units detail::floating_point_promotion_t> fmax(const UnitTypeLhs x, const UnitTypeRhs y) noexcept { using CommonUnit = decltype(units::fmax(x, y)); - return CommonUnit(std::fmax(CommonUnit(x).value(), CommonUnit(y).value())); + return CommonUnit(std::fmax(CommonUnit(x).raw(), CommonUnit(y).raw())); } /** @@ -3903,7 +3903,7 @@ namespace units detail::floating_point_promotion_t> fmin(const UnitTypeLhs x, const UnitTypeRhs y) noexcept { using CommonUnit = decltype(units::fmin(x, y)); - return CommonUnit(std::fmin(CommonUnit(x).value(), CommonUnit(y).value())); + return CommonUnit(std::fmin(CommonUnit(x).raw(), CommonUnit(y).raw())); } //---------------------------------- @@ -3920,7 +3920,7 @@ namespace units template, int> = 0> detail::floating_point_promotion_t fabs(const UnitType x) noexcept { - return detail::floating_point_promotion_t(std::fabs(x.value())); + return detail::floating_point_promotion_t(std::fabs(x.raw())); } /** @@ -3933,7 +3933,7 @@ namespace units template, int> = 0> UnitType abs(const UnitType x) noexcept { - return UnitType(std::abs(x.value())); + return UnitType(std::abs(x.raw())); } /** @@ -4058,4 +4058,4 @@ namespace units #endif #endif -#endif // units_core_h__ \ No newline at end of file +#endif // units_core_h__ diff --git a/unitTests/main.cpp b/unitTests/main.cpp index 8e516ecd..cbdb9d2d 100644 --- a/unitTests/main.cpp +++ b/unitTests/main.cpp @@ -4381,6 +4381,10 @@ TEST_F(UnitMath, min) const meters d(1); const centimeters e(99); EXPECT_EQ(e, units::min(d, e)); + + const percent f(1); + const percent g(99); + EXPECT_EQ(f, units::min(f, g)); } TEST_F(UnitMath, max) @@ -4392,6 +4396,10 @@ TEST_F(UnitMath, max) meters d(1); centimeters e(101); EXPECT_EQ(e, max(d, e)); + + percent f(1); + percent g(101); + EXPECT_EQ(g, max(f, g)); } TEST_F(UnitMath, cos) @@ -4717,6 +4725,9 @@ TEST_F(UnitMath, sqrt) EXPECT_TRUE((std::is_same_v, decltype(sqrt(square_meters(4.0)))>)); EXPECT_NEAR(meters(2.0).to(), sqrt(square_meters(4.0)).to(), 5.0e-9); + EXPECT_TRUE((std::is_same_v, decltype(sqrt(percent(4.0) * percent(4.0)))>)); + EXPECT_NEAR(percent(2.0).raw(), sqrt(percent(4.0)).raw(), 5.0e-9); + EXPECT_TRUE((std::is_same_v, decltype(sqrt(steradians(16.0)))>)); EXPECT_NEAR(angle::radians(4.0).to(), sqrt(steradians(16.0)).to(), 5.0e-9); @@ -4743,12 +4754,16 @@ TEST_F(UnitMath, ceil) double val = 101.1; EXPECT_EQ(ceil(val), ceil(meters(val)).to()); EXPECT_TRUE((std::is_same_v, decltype(ceil(meters(val)))>)); + + EXPECT_EQ(ceil(val), ceil(percent(val)).raw()); + EXPECT_TRUE((std::is_same_v, decltype(ceil(percent(val)))>)); } TEST_F(UnitMath, floor) { double val = 101.1; EXPECT_EQ(floor(val), floor(dimensionless(val))); + EXPECT_EQ(floor(val), floor(percent(val)).raw()); } TEST_F(UnitMath, fmod) @@ -4760,12 +4775,14 @@ TEST_F(UnitMath, trunc) { double val = 101.1; EXPECT_EQ(trunc(val), trunc(dimensionless(val))); + EXPECT_EQ(trunc(val), trunc(percent(val)).raw()); } TEST_F(UnitMath, round) { double val = 101.1; EXPECT_EQ(round(val), round(dimensionless(val))); + EXPECT_EQ(round(val), round(percent(val)).raw()); } TEST_F(UnitMath, copysign) @@ -4776,12 +4793,22 @@ TEST_F(UnitMath, copysign) EXPECT_EQ(meters(-5.0), copysign(val, angle::radians(sign))); } +TEST_F(UnitMath, copysign_percent) +{ + double sign = -1; + percent val(5.0); + EXPECT_EQ(percent(-5.0), copysign(val, sign)); +} + TEST_F(UnitMath, fdim) { EXPECT_EQ(meters(0.0), fdim(meters(8.0), meters(10.0))); EXPECT_EQ(meters(2.0), fdim(meters(10.0), meters(8.0))); EXPECT_NEAR(meters(9.3904).to(), meters(fdim(meters(10.0), feet(2.0))).to(), 5.0e-320); // not sure why they aren't comparing exactly equal, but clearly they are. + + EXPECT_EQ(percent(0.0), fdim(percent(8.0), percent(10.0))); + EXPECT_EQ(percent(2.0), fdim(percent(10.0), percent(8.0))); } TEST_F(UnitMath, fmin) @@ -4789,6 +4816,9 @@ TEST_F(UnitMath, fmin) EXPECT_EQ(meters(8.0), fmin(meters(8.0), meters(10.0))); EXPECT_EQ(meters(8.0), fmin(meters(10.0), meters(8.0))); EXPECT_EQ(feet(2.0), fmin(meters(10.0), feet(2.0))); + + EXPECT_EQ(percent(8.0), fmin(percent(8.0), percent(10.0))); + EXPECT_EQ(percent(8.0), fmin(percent(10.0), percent(8.0))); } TEST_F(UnitMath, fmax) @@ -4796,18 +4826,27 @@ TEST_F(UnitMath, fmax) EXPECT_EQ(meters(10.0), fmax(meters(8.0), meters(10.0))); EXPECT_EQ(meters(10.0), fmax(meters(10.0), meters(8.0))); EXPECT_EQ(meters(10.0), fmax(meters(10.0), feet(2.0))); + + EXPECT_EQ(percent(10.0), fmax(percent(8.0), percent(10.0))); + EXPECT_EQ(percent(10.0), fmax(percent(10.0), percent(8.0))); } TEST_F(UnitMath, fabs) { EXPECT_EQ(meters(10.0), fabs(meters(-10.0))); EXPECT_EQ(meters(10.0), fabs(meters(10.0))); + + EXPECT_EQ(percent(10.0), fabs(percent(-10.0))); + EXPECT_EQ(percent(10.0), fabs(percent(10.0))); } TEST_F(UnitMath, abs) { EXPECT_EQ(meters(10.0), abs(meters(-10.0))); EXPECT_EQ(meters(10.0), abs(meters(10.0))); + + EXPECT_EQ(percent(10.0), abs(percent(-10.0))); + EXPECT_EQ(percent(10.0), abs(percent(10.0))); } TEST_F(UnitMath, fma) @@ -4832,6 +4871,20 @@ TEST_F(UnitMath, isnan) EXPECT_TRUE(units::isnan(inf - inf)); } +TEST_F(UnitMath, isnan_percent) +{ + percent<> zero(0.0); + percent<> nan(NAN); + percent<> inf(INFINITY); + + EXPECT_TRUE(units::isnan(nan)); + EXPECT_FALSE(units::isnan(inf)); + EXPECT_FALSE(units::isnan(zero)); + EXPECT_FALSE(units::isnan(DBL_MIN / 2.0 * 1_pct)); + EXPECT_TRUE(units::isnan(zero / zero)); + EXPECT_TRUE(units::isnan(inf - inf)); +} + TEST_F(UnitMath, isinf) { meters<> zero(0.0); @@ -4845,6 +4898,17 @@ TEST_F(UnitMath, isinf) EXPECT_FALSE(units::isinf(DBL_MIN / 2.0 * 1_m)); } +TEST_F(UnitMath, isinf_percent) +{ + percent<> zero(0.0); + percent<> nan(NAN); + percent<> inf(INFINITY); + + EXPECT_FALSE(units::isinf(nan)); + EXPECT_TRUE(units::isinf(inf)); + EXPECT_FALSE(units::isinf(zero)); +} + TEST_F(UnitMath, isfinite) { meters<> zero(0.0); @@ -4858,6 +4922,18 @@ TEST_F(UnitMath, isfinite) EXPECT_TRUE(units::isfinite(DBL_MIN / 2.0 * 1_m)); } +TEST_F(UnitMath, isfinite_percent) +{ + percent<> zero(0.0); + percent<> nan(NAN); + percent<> inf(INFINITY); + + EXPECT_FALSE(units::isfinite(nan)); + EXPECT_FALSE(units::isfinite(inf)); + EXPECT_TRUE(units::isfinite(zero)); + EXPECT_TRUE(units::isfinite(DBL_MIN / 2.0 * 1_pct)); +} + TEST_F(UnitMath, isnormal) { meters<> zero(0.0); @@ -4870,6 +4946,18 @@ TEST_F(UnitMath, isnormal) EXPECT_TRUE(units::isnormal(1.0_m)); } +TEST_F(UnitMath, isnormal_percent) +{ + percent<> zero(0.0); + percent<> nan(NAN); + percent<> inf(INFINITY); + + EXPECT_FALSE(units::isnormal(nan)); + EXPECT_FALSE(units::isnormal(inf)); + EXPECT_FALSE(units::isnormal(zero)); + EXPECT_TRUE(units::isnormal(1.0_pct)); +} + TEST_F(UnitMath, isunordered) { meters<> zero(0.0); @@ -4881,6 +4969,17 @@ TEST_F(UnitMath, isunordered) EXPECT_FALSE(units::isunordered(zero, zero)); } +TEST_F(UnitMath, isunordered_percent) +{ + percent<> zero(0.0); + percent<> nan(NAN); + percent<> inf(INFINITY); + + EXPECT_TRUE(units::isunordered(nan, zero)); + EXPECT_TRUE(units::isunordered(zero, nan)); + EXPECT_FALSE(units::isunordered(zero, zero)); +} + // Constexpr TEST_F(Constexpr, construction) { @@ -5090,4 +5189,4 @@ int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} \ No newline at end of file +}