diff --git a/dune/xt/common/float_cmp.hh b/dune/xt/common/float_cmp.hh index 9545b7f41ea95054c451e97c9f6c450b474ce627..53d0b4da0a1366141dbe0ac86963599416f1bcdf 100644 --- a/dune/xt/common/float_cmp.hh +++ b/dune/xt/common/float_cmp.hh @@ -77,7 +77,8 @@ struct DefaultEpsilon<std::complex<T>, Style::numpy, false> template <class T, Style style> struct DefaultEpsilon<T, style, true> { - static long value() + typedef T Type; + static T value() { return std::numeric_limits<T>::epsilon(); } @@ -86,7 +87,8 @@ struct DefaultEpsilon<T, style, true> template <class T> struct DefaultEpsilon<T, Style::numpy, true> { - static long value() + typedef T Type; + static T value() { return std::numeric_limits<T>::epsilon(); } diff --git a/dune/xt/common/float_cmp_internal.hh b/dune/xt/common/float_cmp_internal.hh index 38bc9935e4bc7551db43257b5445c078c045b9e0..4b1ae89aab6bfa3c1642179016bf9be7243f0b48 100644 --- a/dune/xt/common/float_cmp_internal.hh +++ b/dune/xt/common/float_cmp_internal.hh @@ -17,6 +17,7 @@ #include <dune/common/float_cmp.hh> +#include <dune/xt/common/math.hh> #include <dune/xt/common/type_traits.hh> #include <dune/xt/common/float_cmp_style.hh> @@ -45,7 +46,6 @@ struct MatrixAbstraction; template <class MatType> struct is_matrix; - namespace FloatCmp { namespace internal { @@ -57,7 +57,7 @@ template <class T> typename std::enable_if<std::is_arithmetic<T>::value, bool>::type float_cmp_eq(const T& xx, const T& yy, const T& rtol, const T& atol) { - return std::abs(xx - yy) <= atol + std::abs(yy) * rtol; + return absolute_difference(xx, yy) <= atol + Dune::XT::Common::abs(yy) * rtol; } template <class T> diff --git a/dune/xt/common/math.hh b/dune/xt/common/math.hh index 723326a4ed430b145601dc347da2cd00926fd18f..17e0c9f0d3f0fc062df23c75e3cdd7452f0e932f 100644 --- a/dune/xt/common/math.hh +++ b/dune/xt/common/math.hh @@ -71,10 +71,11 @@ struct Epsilon<std::string, false> }; template <class T> -const T Epsilon<T, true>::value = 1; +const T Epsilon<T, true>::value = T(1); template <class T> const T Epsilon<T, false>::value = std::numeric_limits<T>::epsilon(); +namespace internal { /** * Helper struct to compute absolute values of signed and unsigned values, * std::abs is only defined for signed types. @@ -108,11 +109,21 @@ struct Absretval<T, true> typedef typename underlying_type<T>::type type; }; +} // namespace internal + +//! drop-in replacement for std::abs, that works for more types +template <class T> +typename internal::Absretval<T>::type abs(const T& val) +{ + typedef typename internal::Absretval<T>::type R; + return internal::AbsoluteValue<R>::result(static_cast<R>(val)); +} + +//! very simple, underrun-safe for unsigned types, difference method template <class T> -typename Absretval<T>::type abs(const T& val) +T absolute_difference(T a, T b) { - typedef typename Absretval<T>::type R; - return AbsoluteValue<R>::result(static_cast<R>(val)); + return (a > b) ? a - b : b - a; } //! a vector wrapper for continiously updating min,max,avg of some element type vector diff --git a/dune/xt/common/ranges.hh b/dune/xt/common/ranges.hh index d552812faa934a84dd7d726effc76503dd55f6d2..c6c5f0b0dd0fbd2554048d0298bae554dda7ea85 100644 --- a/dune/xt/common/ranges.hh +++ b/dune/xt/common/ranges.hh @@ -138,13 +138,13 @@ value_range(const T start, const T end, const T increment = Epsilon<T>::value) } //! signature for enumeration Types T -template <class T, class sequence = std::vector<typename Absretval<T>::type>> -typename std::enable_if<std::is_enum<T>::value, sequence>::type -value_range(const T start, - const T end, - const typename Absretval<T>::type increment = Epsilon<typename Absretval<T>::type>::value) +template <class T, class sequence = std::vector<typename internal::Absretval<T>::type>> +typename std::enable_if<std::is_enum<T>::value, sequence>::type value_range( + const T start, + const T end, + const typename internal::Absretval<T>::type increment = Epsilon<typename internal::Absretval<T>::type>::value) { - typedef typename Absretval<T>::type R; + typedef typename internal::Absretval<T>::type R; return value_range(static_cast<R>(start), static_cast<R>(end), increment); } @@ -156,7 +156,7 @@ typename std::enable_if<!std::is_enum<T>::value, sequence>::type value_range(con } //! get a vector with values in [0 : Epsilon<T> : end) -template <class T, class sequence = std::vector<typename Absretval<T>::type>> +template <class T, class sequence = std::vector<typename internal::Absretval<T>::type>> typename std::enable_if<std::is_enum<T>::value, sequence>::type value_range(const T end) { return value_range(T(0), end); diff --git a/dune/xt/common/test/float_cmp.cc b/dune/xt/common/test/float_cmp.cc index 6837220db5683677491a61912dc5c68908717953..fef1fe998a229a5d802fe66831c08f425bd5fd81 100644 --- a/dune/xt/common/test/float_cmp.cc +++ b/dune/xt/common/test/float_cmp.cc @@ -25,21 +25,15 @@ using namespace Dune; using XT::Common::create; using XT::Common::FloatCmp::Style; -using XT::Common::VectorAbstraction; -using XT::Common::MatrixAbstraction; -static const Style numpy = Style::numpy; -static const Style relativeWeak = Style::relativeWeak; -static const Style relativeStrong = Style::relativeStrong; -static const Style absolute = Style::absolute; -static const Style defaultStyle = Style::defaultStyle; struct FloatCmpTest : public testing::Test { typedef TESTTYPE V; - static const size_t s_size = VectorAbstraction<V>::has_static_size ? VectorAbstraction<V>::static_size : VECSIZE; + static const size_t s_size = + XT::Common::VectorAbstraction<V>::has_static_size ? XT::Common::VectorAbstraction<V>::static_size : VECSIZE; - typedef typename VectorAbstraction<V>::ScalarType S; - typedef typename VectorAbstraction<V>::RealType R; + typedef typename XT::Common::VectorAbstraction<V>::ScalarType S; + typedef typename XT::Common::VectorAbstraction<V>::RealType R; static constexpr bool fieldtype_is_float = std::is_floating_point<R>::value; FloatCmpTest() diff --git a/dune/xt/common/test/float_cmp.mini b/dune/xt/common/test/float_cmp.mini index fe0cbedaa4913d9303ad8e2a61fbb89032de4dd2..02498d407df41829b672ae9d0ce9b4df38b1d81a 100644 --- a/dune/xt/common/test/float_cmp.mini +++ b/dune/xt/common/test/float_cmp.mini @@ -9,8 +9,8 @@ __local.cmpstyle_template = , <Style::{__local.cmpstyles}> | expand cmpstyle_tem __local.cmpstyle_template_short = none, {__local.cmpstyles} | expand cmpstyle_templ __local.default_eps = DefaultEpsilon<S>, DefaultEpsilon<S\,Style::{__local.cmpstyles}> | expand cmpstyle_templ -__local.fieldtype = std::size_t, long, double, std::complex<double> | expand field -__local.fieldtype_short = std_size_t, long, double, complex | expand field +__local.fieldtype = std::size_t, long, double, std::complex<double>, unsigned | expand field +__local.fieldtype_short = std_size_t, long, double, complex, unsigned | expand field __local.vectortype = std::vector<{__local.fieldtype}>, Dune::FieldVector<{__local.fieldtype}\,{__local.vec_size}>, Dune::XT::Common::FieldVector<{__local.fieldtype}\,{__local.vec_size}>, Dune::DynamicVector<{__local.fieldtype}> | expand vector __local.vectortype_short = std_vector, dune_fieldvector, xt_fieldvector, dynamic_vector | expand vector __local.testtype = {__local.fieldtype}, {__local.vectortype} | expand test diff --git a/dune/xt/common/vector.hh b/dune/xt/common/vector.hh index ae7392988a7d3cb70e4ae59cb088752cbf01c373..61c0bcea963dc616c2d3566dffcbdded82046e6f 100644 --- a/dune/xt/common/vector.hh +++ b/dune/xt/common/vector.hh @@ -23,7 +23,6 @@ #include <dune/xt/common/exceptions.hh> #include <dune/xt/common/type_traits.hh> -#include <dune/xt/common/float_cmp.hh> namespace Dune { namespace XT {