From 0cc26f4f46d9d5ea2a98e7c5cdf90ee19bd8d37c Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 29 Nov 2024 13:21:34 +0100 Subject: [PATCH] feat: `abs(quantity)` exposed for conforming freestanding implementations --- src/core/include/mp-units/math.h | 48 +++++++++++++++++++------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/core/include/mp-units/math.h b/src/core/include/mp-units/math.h index 8b9e63a70..70c418109 100644 --- a/src/core/include/mp-units/math.h +++ b/src/core/include/mp-units/math.h @@ -22,8 +22,6 @@ #pragma once -#include -// #include #include #include @@ -35,14 +33,38 @@ #ifdef MP_UNITS_IMPORT_STD import std; #else -#include #include +#include #include -#endif -#endif +#if MP_UNITS_HOSTED +#include +#endif // MP_UNITS_HOSTED +#endif // MP_UNITS_IMPORT_STD +#endif // MP_UNITS_IN_MODULE_INTERFACE MP_UNITS_EXPORT namespace mp_units { + +#if MP_UNITS_HOSTED || __cpp_lib_freestanding_cstdlib >= 202306L + +/** + * @brief Computes the absolute value of a quantity + * + * @param q Quantity being the base of the operation + * @return Quantity The absolute value of a provided quantity + */ +template + requires requires(Rep v) { abs(v); } || requires(Rep v) { std::abs(v); } +[[nodiscard]] constexpr quantity abs(const quantity& q) noexcept +{ + using std::abs; + return {static_cast(abs(q.numerical_value_ref_in(q.unit))), R}; +} + +#endif + +#if MP_UNITS_HOSTED + /** * @brief Computes the value of a quantity raised to the `Num/Den` power * @@ -122,20 +144,6 @@ template auto R, typename Rep> quantity{static_cast(exp(q.force_numerical_value_in(q.unit))), detail::clone_reference_with(R)}); } -/** - * @brief Computes the absolute value of a quantity - * - * @param q Quantity being the base of the operation - * @return Quantity The absolute value of a provided quantity - */ -template - requires requires(Rep v) { abs(v); } || requires(Rep v) { std::abs(v); } -[[nodiscard]] constexpr quantity abs(const quantity& q) noexcept -{ - using std::abs; - return {static_cast(abs(q.numerical_value_ref_in(q.unit))), R}; -} - /** * @brief Determines if a quantity is finite. * @@ -481,4 +489,6 @@ template return quantity{hypot(x.numerical_value_in(unit), y.numerical_value_in(unit), z.numerical_value_in(unit)), ref}; } +#endif // MP_UNITS_HOSTED + } // namespace mp_units