From 621faae8bcb63de94ccf6c1d92d285b6220b5f96 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 1 Jun 2023 08:49:08 +0200 Subject: [PATCH] feat: `sqrt()` and `cbrt()` added for `dimension`, `quantity_spec`, and `unit` --- src/core/include/mp-units/dimension.h | 19 +++++++++++++++++++ src/core/include/mp-units/quantity_spec.h | 21 +++++++++++++++++++++ src/core/include/mp-units/unit.h | 18 ++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/src/core/include/mp-units/dimension.h b/src/core/include/mp-units/dimension.h index 3798f199..4c9c0796 100644 --- a/src/core/include/mp-units/dimension.h +++ b/src/core/include/mp-units/dimension.h @@ -189,6 +189,25 @@ template detail::type_list_of_base_dimension_less>(d); } +/** + * @brief Computes the square root of a dimension + * + * @param d Dimension being the base of the operation + * + * @return Dimension The result of computation + */ +[[nodiscard]] consteval Dimension auto sqrt(Dimension auto d) { return pow<1, 2>(d); } + +/** + * @brief Computes the cubic root of a dimension + * + * @param d Dimension being the base of the operation + * + * @return Dimension The result of computation + */ +[[nodiscard]] consteval Dimension auto cbrt(Dimension auto d) { return pow<1, 3>(d); } + + // TODO consider adding the support for text output of the dimensional equation } // namespace mp_units diff --git a/src/core/include/mp-units/quantity_spec.h b/src/core/include/mp-units/quantity_spec.h index ebb188c3..89e27c7b 100644 --- a/src/core/include/mp-units/quantity_spec.h +++ b/src/core/include/mp-units/quantity_spec.h @@ -556,6 +556,27 @@ template derived_quantity_spec, Num, Den>>{}); } + +/** + * @brief Computes the square root of a quantity specification + * + * @param q Quantity specification being the base of the operation + * + * @return QuantitySpec The result of computation + */ +[[nodiscard]] consteval QuantitySpec auto sqrt(QuantitySpec auto q) { return pow<1, 2>(q); } + + +/** + * @brief Computes the cubic root of a quantity specification + * + * @param q Quantity specification being the base of the operation + * + * @return QuantitySpec The result of computation + */ +[[nodiscard]] consteval QuantitySpec auto cbrt(QuantitySpec auto q) { return pow<1, 3>(q); } + + namespace detail { enum class specs_convertible_result { no, cast, explicit_conversion, yes }; diff --git a/src/core/include/mp-units/unit.h b/src/core/include/mp-units/unit.h index f3d21baa..63ddbef8 100644 --- a/src/core/include/mp-units/unit.h +++ b/src/core/include/mp-units/unit.h @@ -569,6 +569,24 @@ template } } +/** + * @brief Computes the square root of a unit + * + * @param u Unit being the base of the operation + * + * @return Unit The result of computation + */ +[[nodiscard]] consteval Unit auto sqrt(Unit auto u) { return pow<1, 2>(u); } + +/** + * @brief Computes the cubic root of a unit + * + * @param u Unit being the base of the operation + * + * @return Unit The result of computation + */ +[[nodiscard]] consteval Unit auto cbrt(Unit auto u) { return pow<1, 3>(u); } + /** * @brief Computes the square power of a unit *