From 772a3daaf8562c89c564a18fd9347e08e7a1db9e Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 13 Sep 2021 16:51:37 -0700 Subject: [PATCH] Inverse unit multiplier conversion (#166) * On more_atomic_units: inverse_units_test * Add a test for converting to inverse units with multipliers * Automated formatting of repo files (#170) Co-authored-by: Philip Top Co-authored-by: HELICS-bot Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: HELICS-bot --- test/test_conversions1.cpp | 5 +++++ units/units.hpp | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/test_conversions1.cpp b/test/test_conversions1.cpp index 9ca2720a..99192d99 100644 --- a/test/test_conversions1.cpp +++ b/test/test_conversions1.cpp @@ -631,6 +631,11 @@ TEST(UnitDefinitions, inversionConversion) EXPECT_NEAR(convert(0.5, m, m.inv()), 2.0, test::tolerance); EXPECT_NEAR(convert(10, Hz, s), 0.1, test::tolerance); + + EXPECT_FLOAT_EQ(convert(1.0, kilo * Hz, milli * s), 1.0); + EXPECT_FLOAT_EQ(convert(1.0, milli * s, Hz), 1000.0); + EXPECT_FLOAT_EQ(convert(50.0, micro * s, kilo * Hz), 20.0); + EXPECT_FLOAT_EQ(convert(20000, kilo * Hz, nano * s), 50.0); } TEST(quickConvert, simple) diff --git a/units/units.hpp b/units/units.hpp index 300870a6..8bc19215 100644 --- a/units/units.hpp +++ b/units/units.hpp @@ -134,7 +134,7 @@ double convert(double val, const UX& start, const UX2& result) if (base_start.has_same_base(base_result.inv())) { // ignore flag and e flag special cases have been dealt with already, // so those are just markers - return result.multiplier() / (val * start.multiplier()); + return 1.0 / (val * start.multiplier() * result.multiplier()); } if (start.has_e_flag() || result.has_e_flag()) { double converted_val =