diff --git a/CHANGELOG.md b/CHANGELOG.md index 89759390..c22cf880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.1.1 * Unified usage of template arugments in unit_t +* electric conductance can be built from 1/Ohm (including all ratios) ## 1.1.0 diff --git a/README.md b/README.md index 42f324be..0a0a78b9 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ All units that can be built from other units are also decayable to the respectiv | 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 conductance | G | S | I / U | aS to ES | `*_siemens_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` | | Pressure | p | pa | F / L^2 | apa to Epa | `*_pascal_t` | diff --git a/include/SI/electric_conductance.h b/include/SI/electric_conductance.h index 4588d21f..62078f08 100644 --- a/include/SI/electric_conductance.h +++ b/include/SI/electric_conductance.h @@ -15,6 +15,7 @@ #include "detail/unit.h" #include "electric_current.h" #include "electric_potential.h" +#include "electric_resistance.h" namespace SI { @@ -50,9 +51,18 @@ template using exa_siemens_t = electric_conductance_t<_Type, std::exa>; namespace detail { -/// @todo add building from 1/Ohm + BUILD_UNIT_FROM_DIVISON(electric_conductance_t, electric_current_t, electric_potential_t) + +/// Builds conductance from 1/resistance +template > +constexpr auto +operator/(const _Type scalar, + const electric_resistance_t<_Type, _Ratio> &resistance) { + return electric_conductance_t<_Type, std::ratio<_Ratio::den, _Ratio::num>>{ + scalar / resistance.raw_value()}; +} } // namespace detail inline namespace literals { diff --git a/test/src/derived_unit_tests/electric_conductance_tests.cc b/test/src/derived_unit_tests/electric_conductance_tests.cc index 5b37d0d1..8c4b9553 100644 --- a/test/src/derived_unit_tests/electric_conductance_tests.cc +++ b/test/src/derived_unit_tests/electric_conductance_tests.cc @@ -1,6 +1,7 @@ #include #include +#include using namespace SI::literals; TEST_CASE("GIVEN a value WHEN constructed with literal _aS THEN result is a " @@ -198,7 +199,7 @@ TEMPLATE_TEST_CASE("GIVEN a electric_conductance value WHEN multiplied by an " TEMPLATE_TEST_CASE("GIVEN a electric_current value WHEN divided by a " "electric_conductance value THEN " "result is an electric_potential value", - "[electric_current][operator/]", int64_t, long double) { + "[electric_conductance][operator/]", int64_t, long double) { constexpr SI::electric_current_t> electric_current{1}; constexpr SI::electric_conductance_t> electric_conductance{1}; @@ -209,3 +210,45 @@ TEMPLATE_TEST_CASE("GIVEN a electric_current value WHEN divided by a " std::is_same>>::value); } + +TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a resistance value THEN " + "result is an electric conductance value", + "[electric_conductance][operator/]", int64_t, long double) { + + constexpr SI::electric_resistance_t> resistance{5}; + constexpr TestType scalar{10}; + + constexpr auto result = scalar / resistance; + constexpr SI::siemens_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 resistance value THEN " + "result is electric_conductance AND ratio is kept", + "[electric_conductance][operator/]", + (ratio_tuple), + (ratio_tuple), + (ratio_tuple, std::ratio<1>>)) { + + constexpr auto resistance = + SI::electric_resistance_t{5}; + constexpr int64_t scalar{10}; + + constexpr auto result = scalar / resistance; + constexpr SI::electric_conductance_t + expected{2}; + STATIC_REQUIRE(std::ratio_equal::value); + STATIC_REQUIRE(result == expected); +} \ No newline at end of file