From d365215a20613c131b0f139f11b08c59e3591a3e Mon Sep 17 00:00:00 2001 From: Felix Schindler <felix.schindler@wwu.de> Date: Tue, 23 Jan 2018 10:42:16 +0100 Subject: [PATCH] [math] add min/max functions with type promotion --- dune/xt/common/math.hh | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/dune/xt/common/math.hh b/dune/xt/common/math.hh index d5d732d7c..10bf97aaf 100644 --- a/dune/xt/common/math.hh +++ b/dune/xt/common/math.hh @@ -22,8 +22,6 @@ #include <type_traits> #include <complex> -#include <dune/common/deprecated.hh> - #include <dune/xt/common/disable_warnings.hh> #include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/statistics/stats.hpp> @@ -37,6 +35,9 @@ #include <boost/static_assert.hpp> #include <dune/xt/common/reenable_warnings.hh> +#include <dune/common/deprecated.hh> +#include <dune/common/promotiontraits.hh> + #include <dune/xt/common/type_traits.hh> #include <dune/xt/common/vector.hh> @@ -44,6 +45,7 @@ namespace Dune { namespace XT { namespace Common { + /** * since std::numeric_limits<T>::epsilon() is 0 for integral types * use this to get the minimum increment/difference for all basic types @@ -318,6 +320,34 @@ public: }; +template <class T> +T max(const T& left, const T& right) +{ + return std::max(left, right); +} + +template <class L, class R> +typename PromotionTraits<L, R>::PromotedType max(const L& left, const R& right) +{ + using T = typename PromotionTraits<L, R>::PromotedType; + return std::max(T(left), T(right)); +} + + +template <class T> +T min(const T& left, const T& right) +{ + return std::min(left, right); +} + +template <class L, class R> +typename PromotionTraits<L, R>::PromotedType min(const L& left, const R& right) +{ + using T = typename PromotionTraits<L, R>::PromotedType; + return std::min(T(left), T(right)); +} + + } // namespace Common } // namespace XT } // namespace Dune -- GitLab