From 150b67f88729d46cf514df430edf7be2c14d861c Mon Sep 17 00:00:00 2001 From: Timo Koch <timo.koch@iws.uni-stuttgart.de> Date: Thu, 22 Feb 2018 14:21:15 +0100 Subject: [PATCH] [math] Improve signature of harmonic/geometricMean Add constexpr (not for geometricMean which uses std::sqrt) and noexcept. Static assert the requirements on the input arguments, they have to be numbers. --- dumux/common/math.hh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dumux/common/math.hh b/dumux/common/math.hh index 905d5aab72..e076ffac73 100644 --- a/dumux/common/math.hh +++ b/dumux/common/math.hh @@ -42,8 +42,10 @@ namespace Dumux * \param y The second input value */ template <class Scalar> -Scalar harmonicMean(Scalar x, Scalar y) +constexpr Scalar harmonicMean(Scalar x, Scalar y) noexcept { + static_assert(Dune::IsNumber<Scalar>::value, "The arguments x and y have to be numbers!"); + if (x*y <= 0) return 0; return (2*x*y)/(x + y); @@ -55,10 +57,13 @@ Scalar harmonicMean(Scalar x, Scalar y) * * \param x The first input value * \param y The second input value + * \note as std::sqrt is not constexpr this function is not constexpr */ template <class Scalar> -Scalar geometricMean(Scalar x, Scalar y) +Scalar geometricMean(Scalar x, Scalar y) noexcept { + static_assert(Dune::IsNumber<Scalar>::value, "The arguments x and y have to be numbers!"); + if (x*y <= 0) return 0; using std::sqrt; -- GitLab