diff --git a/CHANGELOG.md b/CHANGELOG.md index a339494ba..efe2a403d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - feat: `ppm` parts per million added by [@nebkat](https://github.com/nebkat) - feat: `atan2` 2-argument arctangent added by [@nebkat](https://github.com/nebkat) - feat: `fmod` floating-point division remainder added by [@nebkat](https://github.com/nebkat) +- feat: `remainder` IEEE division remainder added by [@nebkat](https://github.com/nebkat) - feat: `std::format` support added - feat: unit text output support added - feat: formatting error messages improved diff --git a/docs/users_guide/framework_basics/quantity_arithmetics.md b/docs/users_guide/framework_basics/quantity_arithmetics.md index 62a487cc6..929f08770 100644 --- a/docs/users_guide/framework_basics/quantity_arithmetics.md +++ b/docs/users_guide/framework_basics/quantity_arithmetics.md @@ -337,7 +337,7 @@ Among others, we can find there the following: - `exp()`, - `abs()`, - `epsilon()`, -- `fma()`, `fmod()`, +- `fma()`, `fmod()`, `remainder()`, - `isfinite()`, `isinf()`, `isnan()`, - `floor()`, `ceil()`, `round()`, - `inverse()`, diff --git a/src/core/include/mp-units/math.h b/src/core/include/mp-units/math.h index 6620a63ca..ab4f4ae04 100644 --- a/src/core/include/mp-units/math.h +++ b/src/core/include/mp-units/math.h @@ -214,6 +214,23 @@ template return quantity{fmod(x.numerical_value_in(unit), y.numerical_value_in(unit)), ref}; } +/** + * @brief Computes the IEEE remainder of the floating point division operation x / y. + */ +template + requires requires(Rep1 v1, Rep2 v2) { + common_reference(R1, R2); + requires requires { remainder(v1, v2); } || requires { std::remainder(v1, v2); }; + } +[[nodiscard]] constexpr QuantityOf auto remainder(const quantity& x, + const quantity& y) noexcept +{ + constexpr auto ref = common_reference(R1, R2); + constexpr auto unit = get_unit(ref); + using std::remainder; + return quantity{remainder(x.numerical_value_in(unit), y.numerical_value_in(unit)), ref}; +} + /** * @brief Returns the epsilon of the quantity diff --git a/test/runtime/math_test.cpp b/test/runtime/math_test.cpp index 3671ebf80..7eab3fb16 100644 --- a/test/runtime/math_test.cpp +++ b/test/runtime/math_test.cpp @@ -89,6 +89,24 @@ TEST_CASE("fmod functions", "[math][fmod]") } } +TEST_CASE("remainder functions", "[math][remainder]") +{ + SECTION("remainder should work on the same quantities") + { + REQUIRE(remainder(4. * isq::length[km], 3. * isq::length[km]) == 1. * isq::length[km]); + REQUIRE(remainder(-9. * isq::length[km], 3. * isq::length[km]) == -0. * isq::length[km]); + REQUIRE(remainder(3 * isq::length[km], 2 * isq::length[km]) == -1 * isq::length[km]); + REQUIRE(remainder(4 * isq::length[km], 2.75f * isq::length[km]) == 1.25 * isq::length[km]); + } + SECTION("remainder should work with different units of the same dimension") + { + REQUIRE(remainder(4. * isq::length[km], 3000. * isq::length[m]) == 1000. * isq::length[m]); + REQUIRE(remainder(-9. * isq::length[km], 3000. * isq::length[m]) == -0. * isq::length[m]); + REQUIRE(remainder(3. * isq::length[km], 2000. * isq::length[m]) == -1000 * isq::length[m]); + REQUIRE(remainder(4 * isq::length[km], 2750 * isq::length[m]) == 1250 * isq::length[m]); + } +} + TEST_CASE("'isfinite()' accepts dimensioned arguments", "[math][isfinite]") { REQUIRE(isfinite(4.0 * isq::length[m])); } TEST_CASE("'isinf()' accepts dimensioned arguments", "[math][isinf]") { REQUIRE(!isinf(4.0 * isq::length[m])); } diff --git a/test/static/math_test.cpp b/test/static/math_test.cpp index 292a9da6c..68c878486 100644 --- a/test/static/math_test.cpp +++ b/test/static/math_test.cpp @@ -59,6 +59,12 @@ static_assert(compare(fmod(9.0 * km, -3.0 * km), 0.0 * km)); static_assert(compare(fmod(9.5 * km, -2000 * m), 1500.0 * m)); static_assert(compare(fmod(3 * km, 2 * km), 1.0 * km)); static_assert(compare(fmod(4 * km, 2.5f * km), 1.5 * km)); +static_assert(compare(remainder(4.0 * km, 3.0 * km), 1.0 * km)); +static_assert(compare(remainder(-4.0 * km, 3.0 * km), -1.0 * km)); +static_assert(compare(remainder(9.0 * km, -3.0 * km), 0.0 * km)); +static_assert(compare(remainder(9.5 * km, -2000 * m), -500.0 * m)); +static_assert(compare(remainder(3 * km, 2 * km), -1.0 * km)); +static_assert(compare(remainder(4 * km, 2.75f * km), 1.25 * km)); static_assert(compare(pow<0>(2 * m), 1 * one)); static_assert(compare(pow<1>(2 * m), 2 * m)); static_assert(compare(pow<2>(2 * m), 4 * pow<2>(m), 4 * m2));