From 31f15c090bef7463f73751d7ac5debf63766d220 Mon Sep 17 00:00:00 2001 From: Dominik Berner Date: Sat, 3 Aug 2019 22:45:07 +0200 Subject: [PATCH] Conductance buildable from 1/resistance --- CHANGELOG.md | 1 + README.md | 2 +- include/SI/electric_conductance.h | 9 ++-- include/SI/electric_resistance.h | 11 +++++ include/SI/radioactivity.h | 2 + .../electric_conductance_tests.cc | 2 +- .../electric_resistance_tests.cc | 43 +++++++++++++++++++ 7 files changed, 63 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c22cf880..f2ed07fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Unified usage of template arugments in unit_t * electric conductance can be built from 1/Ohm (including all ratios) +* electric resistance can be built from 1/S (including all ratios) ## 1.1.0 diff --git a/README.md b/README.md index 0a0a78b9..71ef1bde 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ All units that can be built from other units are also decayable to the respectiv | Acceleration | a | m/s^2 | v / T | none | none | | Electric charge | Q | C | I \* T | aC to EC | `*_coulomb_t` | | Electric potential | U | V | P / I, E/Q | aV to EV | `*_volt_t` | -| Electric resistance | O* | Ohm (Ω) | U / I | aOhm to EOhm | `*_ohm_t` | +| Electric resistance | O* | Ohm (Ω) | U / I, 1/G | aOhm to EOhm | `*_ohm_t` | | Electric conductance | G | S | I / U, 1/R | aS to ES | `*_siemens_t` | | Electric capacity | C | F | Q / U | aF to EF | `*_farad_t` | | Force | F | N | M \* a | aN to EN | `*_newton_t` | diff --git a/include/SI/electric_conductance.h b/include/SI/electric_conductance.h index 62078f08..b194f165 100644 --- a/include/SI/electric_conductance.h +++ b/include/SI/electric_conductance.h @@ -15,7 +15,6 @@ #include "detail/unit.h" #include "electric_current.h" #include "electric_potential.h" -#include "electric_resistance.h" namespace SI { @@ -55,11 +54,11 @@ namespace detail { BUILD_UNIT_FROM_DIVISON(electric_conductance_t, electric_current_t, electric_potential_t) -/// Builds conductance from 1/resistance +/// Builds conductance from 1/resistance, to avoid include cycles the base +/// unit_t is used instead of the type alias electric_resistance_t template > -constexpr auto -operator/(const _Type scalar, - const electric_resistance_t<_Type, _Ratio> &resistance) { +constexpr auto operator/(const _Type scalar, + const unit_t<'O', 1, _Type, _Ratio> &resistance) { return electric_conductance_t<_Type, std::ratio<_Ratio::den, _Ratio::num>>{ scalar / resistance.raw_value()}; } diff --git a/include/SI/electric_resistance.h b/include/SI/electric_resistance.h index f079956a..8baebaad 100644 --- a/include/SI/electric_resistance.h +++ b/include/SI/electric_resistance.h @@ -17,6 +17,7 @@ #include "electric_potential.h" namespace SI { + /// @todo find a way to use Ohm as dimension symbol template > using electric_resistance_t = detail::unit_t<'O', 1, _Type, _Ratio>; @@ -52,6 +53,16 @@ using exa_ohm_t = electric_resistance_t<_Type, std::exa>; namespace detail { BUILD_UNIT_FROM_DIVISON(electric_resistance_t, electric_potential_t, electric_current_t) + +/// Builds conductance from 1/conductance, to avoid include cycles the base +/// unit_t is used instead of the type alias electric_conductance_t +template > +constexpr auto +operator/(const _Type scalar, + const detail::unit_t<'G', 1, _Type, _Ratio> &conductance) { + return electric_resistance_t<_Type, std::ratio<_Ratio::den, _Ratio::num>>{ + scalar / conductance.raw_value()}; +} } // namespace detail inline namespace literals { diff --git a/include/SI/radioactivity.h b/include/SI/radioactivity.h index e20d6580..66c00ae9 100644 --- a/include/SI/radioactivity.h +++ b/include/SI/radioactivity.h @@ -17,6 +17,8 @@ namespace SI { /// @todo check how to make this the same/an alias of hertz +/// @todo think of how this can be built from 1/T (probably goes along with +/// making it an alias of hertz) template > using radioactivity_t = detail::unit_t<'A', 1, _Type, _Ratio>; diff --git a/test/src/derived_unit_tests/electric_conductance_tests.cc b/test/src/derived_unit_tests/electric_conductance_tests.cc index 8c4b9553..ce180b0a 100644 --- a/test/src/derived_unit_tests/electric_conductance_tests.cc +++ b/test/src/derived_unit_tests/electric_conductance_tests.cc @@ -235,7 +235,7 @@ template struct ratio_tuple { } // namespace TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a resistance value THEN " - "result is electric_conductance AND ratio is kept", + "result is electric_conductance AND ratio is inverse", "[electric_conductance][operator/]", (ratio_tuple), (ratio_tuple), diff --git a/test/src/derived_unit_tests/electric_resistance_tests.cc b/test/src/derived_unit_tests/electric_resistance_tests.cc index 239faf0a..4de77516 100644 --- a/test/src/derived_unit_tests/electric_resistance_tests.cc +++ b/test/src/derived_unit_tests/electric_resistance_tests.cc @@ -1,5 +1,6 @@ #include +#include #include using namespace SI::literals; @@ -217,3 +218,45 @@ TEMPLATE_TEST_CASE("GIVEN a electric_potential value WHEN divided by a " decltype(result), const SI::electric_current_t>>::value); } + +TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a conductance value THEN " + "result is an electric resistance value", + "[electric_resistance][operator/]", int64_t, long double) { + + constexpr SI::electric_conductance_t> conductance{5}; + constexpr TestType scalar{10}; + + constexpr auto result = scalar / conductance; + constexpr SI::ohm_t expected{2}; + STATIC_REQUIRE( + std::is_same>::value); + STATIC_REQUIRE(result == expected); +} + +namespace { +/// helper struct to faciltate templated testcase +template struct ratio_tuple { + using ratio_lhs = _ratio_lhs; + using ratio_rhs = _ratio_rhs; +}; + +} // namespace + +TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a conductance value THEN " + "result is electric_resistance AND ratio is inverse", + "[electric_conductance][operator/]", + (ratio_tuple), + (ratio_tuple), + (ratio_tuple, std::ratio<1>>)) { + + constexpr auto conductance = + SI::electric_conductance_t{5}; + constexpr int64_t scalar{10}; + + constexpr auto result = scalar / conductance; + constexpr SI::electric_resistance_t + expected{2}; + STATIC_REQUIRE(std::ratio_equal::value); + STATIC_REQUIRE(result == expected); +} \ No newline at end of file