From ddbdd6a4c2634d2b13ac2081d38329901137dae9 Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Fri, 10 May 2024 20:05:36 +0200 Subject: [PATCH 1/2] added support for isfinite isinf and isnan on instances of quantity_point --- src/core/include/mp-units/math.h | 62 +++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/core/include/mp-units/math.h b/src/core/include/mp-units/math.h index 90a6539a7..2eabeca42 100644 --- a/src/core/include/mp-units/math.h +++ b/src/core/include/mp-units/math.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -130,10 +131,10 @@ template } /** - * @brief Determines if a number is finite. + * @brief Determines if a quantity is finite. * - * @param a: Number to analyze. - * @return bool: Whether the number is finite or not. + * @param a: Quantity to analyze. + * @return bool: Whether the quantity is finite or not. */ template requires requires(Rep v) { isfinite(v); } || requires(Rep v) { std::isfinite(v); } @@ -144,10 +145,24 @@ template } /** - * @brief Determines if a number is infinite. + * @brief Determines if a quantity point is finite. * - * @param a: Number to analyze. - * @return bool: Whether the number is infinite or not. + * @param a: Quantity point to analyze. + * @return bool: Whether the quantity point is finite or not. + */ +template + requires requires(Rep v) { isfinite(v); } || requires(Rep v) { std::isfinite(v); } +[[nodiscard]] constexpr bool isfinite(const quantity_point& a) noexcept +{ + using std::isfinite; + return isfinite(a.quantity_ref_from(a.point_origin).numerical_value_ref_in(a.unit)); +} + +/** + * @brief Determines if a quantity is infinite. + * + * @param a: Quantity to analyze. + * @return bool: Whether the quantity is infinite or not. */ template requires requires(Rep v) { isinf(v); } || requires(Rep v) { std::isinf(v); } @@ -157,12 +172,26 @@ template return isinf(a.numerical_value_ref_in(a.unit)); } +/** + * @brief Determines if a quantity point is infinite. + * + * @param a: Quantity point to analyze. + * @return bool: Whether the quantity point is infinite or not. + */ +template + requires requires(Rep v) { isinf(v); } || requires(Rep v) { std::isinf(v); } +[[nodiscard]] constexpr bool isinf(const quantity_point& a) noexcept +{ + using std::isinf; + return isinf(a.quantity_ref_from(a.point_origin).numerical_value_ref_in(a.unit)); +} + /** - * @brief Determines if a number is a nan. + * @brief Determines if a quantity is a nan. * - * @param a: Number to analyze. - * @return bool: Whether the number is a NaN or not. + * @param a: Quantity to analyze. + * @return bool: Whether the quantity is a NaN or not. */ template requires requires(Rep v) { isnan(v); } || requires(Rep v) { std::isnan(v); } @@ -172,6 +201,21 @@ template return isnan(a.numerical_value_ref_in(a.unit)); } + +/** + * @brief Determines if a quantity point is a nan. + * + * @param a: Quantity point to analyze. + * @return bool: Whether the quantity point is a NaN or not. + */ +template + requires requires(Rep v) { isnan(v); } || requires(Rep v) { std::isnan(v); } +[[nodiscard]] constexpr bool isnan(const quantity_point& a) noexcept +{ + using std::isnan; + return isnan(a.quantity_ref_from(a.point_origin).numerical_value_ref_in(a.unit)); +} + /** * @brief Computes the fma of 3 quantities * From 086e62c80cd28c9013fe855c698e57ffab3b9c5f Mon Sep 17 00:00:00 2001 From: Yves Delley Date: Fri, 10 May 2024 22:23:52 +0200 Subject: [PATCH 2/2] suggestions from review --- src/core/include/mp-units/math.h | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/core/include/mp-units/math.h b/src/core/include/mp-units/math.h index 2eabeca42..e6957201a 100644 --- a/src/core/include/mp-units/math.h +++ b/src/core/include/mp-units/math.h @@ -151,11 +151,10 @@ template * @return bool: Whether the quantity point is finite or not. */ template - requires requires(Rep v) { isfinite(v); } || requires(Rep v) { std::isfinite(v); } + requires requires(quantity q) { isfinite(q); } [[nodiscard]] constexpr bool isfinite(const quantity_point& a) noexcept { - using std::isfinite; - return isfinite(a.quantity_ref_from(a.point_origin).numerical_value_ref_in(a.unit)); + return isfinite(a.quantity_ref_from(a.point_origin)); } /** @@ -179,11 +178,10 @@ template * @return bool: Whether the quantity point is infinite or not. */ template - requires requires(Rep v) { isinf(v); } || requires(Rep v) { std::isinf(v); } + requires requires(quantity q) { isinf(q); } [[nodiscard]] constexpr bool isinf(const quantity_point& a) noexcept { - using std::isinf; - return isinf(a.quantity_ref_from(a.point_origin).numerical_value_ref_in(a.unit)); + return isinf(a.quantity_ref_from(a.point_origin)); } @@ -209,11 +207,10 @@ template * @return bool: Whether the quantity point is a NaN or not. */ template - requires requires(Rep v) { isnan(v); } || requires(Rep v) { std::isnan(v); } + requires requires(quantity q) { isnan(q); } [[nodiscard]] constexpr bool isnan(const quantity_point& a) noexcept { - using std::isnan; - return isnan(a.quantity_ref_from(a.point_origin).numerical_value_ref_in(a.unit)); + return isnan(a.quantity_ref_from(a.point_origin)); } /**